[英]Cannot redeclare DOMPDF_autoload()
我知道這個問題曾經被問過,但是我還沒有弄清楚應該如何解決。 例如,此DOMPDF,我不能同時創建具有相同主題的兩個pdf 。 我已經嘗試遵循解決方案,但到目前為止還不是很好。 我有一個循環,我想在其中創建兩個pdf文件(兩個發票)。 情況1僅用1個循環工作,在另一個循環中掛起(無法重新聲明DOMPDF_autoload()(先前在第24行的/vendor/dompdf/include/autoload.inc.php中聲明)。情況2和3,我有嘗試從上述stackoverflow問題中應用,根本不起作用(某些運行時錯誤,語法錯誤,我無法弄清楚)。
這是我的代碼:
$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類的結構。 可能的主要解決方案是清除上一個循環的類對象並創建另一個循環,但是如何? 相信我,我已經為此苦了幾天。
我想您在這里問了幾個問題,所以我將回答可能對您影響最大的一個問題,即重用問題。
重用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";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.