我正在尝试使用dompdf从smarty模板生成pdf文件:代码如下:-
require_once('dompdf/dompdf_config.inc.php');
$dompdf = new DOMPDF();
$dompdf->load_html($smarty->fetch(CURRENT_TEMPLATE.'/module/shopping_cart.html'));
$dompdf->render();
$dompdf->stream("sample.pdf");
生成了一个pdf文件,但是当我尝试打开pdf文件时,错误消息显示如下:“ Acrobat无法打开'sample.pdf',因为它不受支持,或者文件已损坏” HTML页面当我尝试正确播放
echo $smarty->fetch(CURRENT_TEMPLATE.'/module/shopping_cart.html')
所以请帮我解决这个错误...谢谢
是的,这是dompdf的问题。但是我能够克服这个问题。我创建了一个用于pdf创建的函数。检查以下功能:
function pdf_create($html, $filename='', $stream=TRUE)
{
require_once("dompdf/dompdf_config.inc.php");
$savein = 'uploads/policy_doc/';
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$canvas = $dompdf->get_canvas();
$font = Font_Metrics::get_font("arial", "normal","12px");
// the same call as in my previous example
$canvas->page_text(540, 773, "Page {PAGE_NUM} of {PAGE_COUNT}",
$font, 6, array(0,0,0));
$pdf = $dompdf->output(); // gets the PDF as a string
file_put_contents($savein.str_replace("/","-",$filename), $pdf); // save the pdf file on server
unset($html);
unset($dompdf);
}
注意:-您需要获取生成的pdf字符串,然后将其保存到pdf文件。
编辑:-您可以从上述功能中删除以下部分:-
$canvas = $dompdf->get_canvas();
$font = Font_Metrics::get_font("arial", "normal","12px");
// the same call as in my previous example
$canvas->page_text(540, 773, "Page {PAGE_NUM} of {PAGE_COUNT}",
$font, 6, array(0,0,0));
上面的代码用于处理多页标头。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句