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

时间:2013-12-19 07:52:25

标签: php pdf-generation smarty dompdf

我正在尝试使用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')

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

5 个答案:

答案 0 :(得分:8)

在致电ob_end_clean();之前使用$dompdf->stream();

答案 1 :(得分:5)

是的,这是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));
  

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

答案 2 :(得分:3)

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

以下是我的代码。

$ dompdf = new DOMPDF();

$ dompdf-> load_html($ HTML);

$ dompdf->渲染();

$ pdf = $ dompdf-> output();

$ invnoabc =' Bokkinglist.pdf';

ob_end_clean();

$ dompdf->流($ invnoabc);出口;

答案 3 :(得分:2)

它对我有用,最后我的代码看起来像:

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

答案 4 :(得分:1)

我对dompdf创建的pdf有问题 我在记事本中打开pdf文件,发现dompdf出现以下php错误

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

发生这种情况是由于dompdf的旧版本无法支持php 7版本 更新dompdf库后,问题得到解决。

下载最新的dompdf库here