假设您将此创建为plugin 您已经将dompdf目录保存在插件目录中,您可以将其包含在以下内容中。
// wp-content/plugins/yourplugin/yourplugin.php
// Set up paths to include DOMPDF
$plugin_path = plugin_dir_path( __FILE__ );
define( \'YOURPLUGIN_DOMPDF\', $plugin_path . \'dompdf/\' );
// Set up directory to save PDF
$upload_dir = wp_upload_dir();
define( \'YOURPLUGIN_UPLOAD_DIR\', $upload_dir[\'basedir\'] . \'/invoices\');
define( \'YOURPLUGIN_UPLOAD_URL\', $upload_dir[\'baseurl\'] . \'/invoices\');
include( YOURPLUGIN_DOMPDF . \'autoload.inc.php\' );
use Dompdf\\Dompdf;
use Dompdf\\Options;
function yourplugin_order_invoice_pdf(){
// validate and sanitize your input
$options = new Options();
$options->set(\'isRemoteEnabled\', true);
$dompdf = new Dompdf($options);
if ( ! $html = yourplugin_generate_invoice_html( $order_id ) ) {
return;
}
$dompdf->load_html($html);
$dompdf->render();
$output = $dompdf->output();
wp_mkdir_p( YOURPLUGIN_UPLOAD_DIR );
$file_name = \'order-\'.$order_id.\'.pdf\';
$file_path = YOURPLUGIN_UPLOAD_DIR . \'/\'. $file_name;
file_put_contents( $file_path, $output);
$invoice_url = YOURPLUGIN_UPLOAD_URL . $file_name;
return $invoice_url;
}
The
yourplugin_order_invoice_pdf
上述功能是所需组件的一个示例。您可能希望将部分拆分为它们自己的特定功能,并处理您的输入。当然,您需要从
Plugin API 将此全部附加到。