【问题标题】:cannot open pdf file generated using dompdf无法打开使用 dompdf 生成的 pdf 文件
【发布时间】:2014-01-07 16:40:24
【问题描述】:

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

所以请帮我解决这个错误...在此先感谢

【问题讨论】:

  • 要使用 stream() 而不损坏文件,请参阅下面的 AntonAL 建议。这是您问题的正确答案;

标签: php pdf-generation smarty dompdf


【解决方案1】:

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

上面这段代码用于处理多页标题。

【讨论】:

  • 感谢您的回答,但不幸的是它对我不起作用..为什么这是 get_canvas();使用过,请您对代码进行一些解释....再次感谢您
  • 您遇到了什么错误?您可以从您的代码中省略 get_canvas。这是使用 - 如果您有多个页面。然后是每个页眉。
  • 非常感谢......它的工作原理:) 无话可说表达我的感谢......我在过去 2 天里一直在追求它......现在是一个很大的解脱
  • 您可以使用 stream() 但看到下面的 AntonAL 建议
【解决方案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);

【讨论】:

    【解决方案3】:

    在调用$dompdf-&gt;stream();之前使用ob_end_clean();。

    【讨论】:

    • 应该选作答案。
    【解决方案4】:

    我也遇到了同样的错误,但是当我在 $dompdf-&gt;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;
    

    【讨论】:

      【解决方案5】:

      我的 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

      【讨论】:

        猜你喜欢
        • 2015-10-30
        • 1970-01-01
        • 2020-07-12
        • 2017-03-17
        • 2017-08-14
        • 2012-06-06
        • 1970-01-01
        • 2018-07-28
        • 1970-01-01
        相关资源
        最近更新 更多