php 无法打开使用dompdf生成的pdf文件

fnx2tebb  于 2022-11-21  发布在  PHP
关注(0)|答案(5)|浏览(593)

我试图使用dompdf从智能模板生成一个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“,因为它不是支持的文件类型或因为文件已损坏”。当我尝试打开PDF文件时,HTML页面显示正确

echo $smarty->fetch(CURRENT_TEMPLATE.'/module/shopping_cart.html')

所以请帮我解决这个错误...提前感谢

8cdiaqws

8cdiaqws1#

在调用$dompdf->stream();之前使用ob_end_clean();
此函数用于丢弃最上层输出缓冲区的内容并关闭此输出缓冲。如果要进一步处理缓冲区的内容,必须在ob_end_clean()之前调用ob_get_contents(),因为调用ob_end_clean()时缓冲区的内容会被丢弃。
PHP manual ob_end_clean

qzwqbdag

qzwqbdag2#

我也得到了同样的错误,但当我在$dompdf->stream()之前使用ob_end_clean()时,它对我有效。
下面是我的代码。

$dompdf = new DOMPDF();
$dompdf->load_html($html); 
$dompdf->render();    
$pdf = $dompdf->output();
$invnoabc = 'Bokkinglist.pdf';
ob_end_clean();
$dompdf->stream($invnoabc);
exit;
wlzqhblo

wlzqhblo3#

是的,这是一个问题的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文件。

EDIT:-您可以从上述功能中删除以下部分:-

$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));

以上代码用于处理多个页面标题。

mfpqipee

mfpqipee4#

对我来说很有效,在我代码结尾看起来像:

$html =
  '<html><body>'.
  '<p>Put your html here, or generate it with your favourite '.
  'templating system.</p>'.
  '</body></html>';
function pdf_create($html, $filename='', $stream=TRUE) 
{
    require_once('scripts/vendor/dompdf/dompdf/dompdf_config.inc.php');
    $savein = '';
    $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("arquivo.pdf",$pdf);  // save the pdf file on server

    unset($html);
    unset($dompdf); 

}

pdf_create($html);
unguejic

unguejic5#

我有问题与我的pdf创建的dompdf我打开pdf文件在记事本++我发现下面的PHP错误与dompdf

Message:  "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"

这是由于旧版本的dompdf不支持php 7版本,更新dompdf库后,问题得到解决。
下载最新的dompdf库here

相关问题