【问题标题】:Cannot redeclare DOMPDF_autoload()无法重新声明 DOMPDF_autoload()
【发布时间】:2016-05-06 06:18:35
【问题描述】:

我知道以前有人问过这个问题,但我还没有弄清楚应该如何解决。例如,DOMPDF, I cannot create two pdf at time 处理相同的主题。我试图遵循解决方案,但到目前为止还不是很好。我有一个循环,我想在其中创建两个 pdf 文件(两张发票)。案例 1 仅适用于 1 个循环,在另一个循环期间它挂起(无法重新声明 DOMPDF_autoload()(之前在第 24 行的 /vendor/dompdf/include/autoload.inc.php 中声明)。案例 2 和 3,我有试图从上面的 stackoverflow quastion 申请,根本不工作(一些运行时错误,我无法弄清楚的语法错误)。

这是我的代码:

$invoices = 2;
$invoice_names = array();
for ($i=1; $i<=$invoices; $i++)
{
$invoice_file_name = 'invoice' .$i. '.pdf';
$invoice_names[] = $invoice_file_name;

require '../vendor/dompdf/include/autoload.inc.php';

// disable DOMPDF's internal autoloader if you are using Composer
define('DOMPDF_ENABLE_AUTOLOAD', false);

// include DOMPDF's default configuration
require_once '../vendor/dompdf/dompdf_config.inc.php';

$htmlString = '';
ob_start();
// This creates html format of an invoice
include('pdf_laskupohja_vanha.php');
$htmlString .= ob_get_clean();

// Case 1 working for the 1. loop
$dompdf = new DOMPDF();
$dompdf->load_html($htmlString);
$dompdf->render();
$output = $dompdf->output();

/* Case 2
$view = $dompdf->load->view("viewname", $htmlString, true);
//create a new dompdf instance
$dompdf->pdf = new DOMPDF();
//render and output pdf
$dompdf->pdf->load_html($view);
$dompdf->pdf->render();
$output = $dompdf->pdf->output(array("compress" => 0));
*/

/* Case 3
$dompdf->load->library('pdf');
$dompdf = new DOMPDF();
$dompdf->set_base_path(['path']);
//create a new dompdf instance (this is the crucial step)
$dompdf->load_view('viewname', $htmlString); 
$dompdf->render();
$output = $dompdf->output();
*/  
file_put_contents('../invoices/' .$invoice_file_name, $output);
}
echo "Invoice(es) OK";

是否有一种“隐藏”的方式来实现这些未直接显示的(案例 2 和 3)?可能包括对 dompdf_config.inc.php 或 DOMPDF 类的一些修改。问题是我并不完全了解 php 类的结构。可能主要的解决方案是清除前一个循环的类对象并创建另一个,但是如何?相信我,我已经为此苦苦挣扎了好几天了。

【问题讨论】:

    标签: php dompdf


    【解决方案1】:

    我想你在这里问了几个问题,所以我会回答可能对你影响最大的问题,即重用问题。

    有一个known issue 可以重用 dompdf 对象实例。如果您只需要渲染几个单独的 PDF,解决方法是只需 unset 您的 $dompdf 变量并重新使用它。

    $dompdf = new DOMPDF();
    $dompdf->load_html('hello world');
    $dompdf->render();
    $pdfHello = $dompdf->output();
    
    unset($dompdf);
    
    $dompdf = new DOMPDF();
    $dompdf->load_html('goodbye world');
    $dompdf->render();
    $pdfBye = $dompdf->output();
    

    另一个值得注意的问题是您应该只包含 dompdf 自动加载器一次,而不是每个循环一次。将 require 语句放在文件的顶部。

    require '../vendor/dompdf/include/autoload.inc.php';
    
    $invoices = 2;
    $invoice_names = array();
    for ($i=1; $i<=$invoices; $i++)
    {
      // PDF-generating code here (NO dompdf require statement)
    }
    echo "Invoice(es) OK";
    

    【讨论】:

    • 感谢您的回答,但(虽然没有提到)我已经尝试了这两种建议。没用。
    • 事实上,我已经尝试了第二次,现在成功了!奇怪的是我第一次没有让它工作(将 autoload.inc.php 放在循环之外)???好吧,这不是我第一次对某事“视而不见” :-|这让我很兴奋......但是谢谢!
    • 我经常有这样的日子:/
    猜你喜欢
    • 2013-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-22
    • 2023-03-29
    • 1970-01-01
    • 2014-07-10
    • 2016-02-24
    相关资源
    最近更新 更多