如何在WordPress中创建我的个人资料的pdf文件

时间:2018-01-26 作者:Ali Zahid

我正在WordPress中创建自定义订单。现在,我想创建发票并下载pdf格式的发票,我如何才能做到这一点。我正在使用dompdf,但它显示了此错误Warning: require_once(dompdf/autoload.inc.php): failed to open stream: No such file or directory in /home/designcrocs/public_html/wp-content/themes/bridge/pdf.php on line 3

如何在WordPress中创建订单的pdf文件?帮帮我。。。

1 个回复
SO网友:Andrew

假设您将此创建为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;
}
Theyourplugin_order_invoice_pdf 上述功能是所需组件的一个示例。您可能希望将部分拆分为它们自己的特定功能,并处理您的输入。当然,您需要从Plugin API 将此全部附加到。

结束

相关推荐