【问题标题】:unable to load pdf in browser dompdf无法在浏览器 dompdf 中加载 pdf
【发布时间】:2016-02-13 08:48:17
【问题描述】:

我们正在尝试使用 DOMPDF 创建 html 到 pdf。当我们将它保存到我们的浏览器时,它是正确创建的。为了创建,我们使用这个函数

function createPDF($pdf_userid, $pdf_content, $pdf_For, $filename){
$path='dompdf/';
/*$rndNumber=rand();
$filename=$pdf_userid.date("Ymd").$rndNumber.'.pdf';*/
$dompdf=new DOMPDF();
$dompdf->load_html($pdf_content);
$dompdf->render();
$output = $dompdf->output();
file_put_contents($path.$filename, $output);
return $filename;       
} 

但是当我们试图在浏览器中显示 pdf 而不保存在服务器上时,它会显示“加载 PDF 失败”。 为此,我们正在使用此代码

 try{
    $dompdf=new DOMPDF();
    $dompdf->load_html($pdf_content);
    $dompdf->render();
    //$output = $dompdf->output();
    $dompdf->stream("dompdf_out.pdf", array("Attachment" => false));
    exit(0);
    }catch(exception $e){
     print_r($e);}

请告诉我们第二个代码有什么问题?

【问题讨论】:

  • 没有 PHP 错误/警告?您的阅读器/浏览器是否出现 PDF 错误?您可以尝试在文本编辑器(例如 TextEdit 或记事本)中打开 PDF。有时 PHP 警告会在 PDF 流中捕获。

标签: php pdf dompdf


【解决方案1】:

试试这个并从 github 下载最新稳定版本的 dompdf

<?php
    // include autoloader
    require_once 'dompdf/autoload.inc.php';

    // reference the Dompdf namespace
    use Dompdf\Dompdf;
    use Dompdf\Options;

    $options = new Options();
    $options->set('isRemoteEnabled', TRUE);

    // instantiate and use the dompdf class
    $dompdf = new Dompdf($options);

    $context = stream_context_create([ 
        'ssl' => [ 
            'verify_peer' => FALSE, 
            'verify_peer_name' => FALSE,
            'allow_self_signed'=> TRUE 
        ] 
    ]);
    $dompdf->setHttpContext($context);

    $html = file_get_contents("html.html");

    $dompdf->loadHtml($html);

    // (Optional) Setup the paper size and orientation
    $dompdf->setPaper('A4', 'landscape');

    // Render the HTML as PDF
    $dompdf->render();

    // Output the generated PDF to browser
    $dompdf->stream();

    // Output the generated PDF (1 = download and 0 = preview)
    $dompdf->stream("codexworld",array("Attachment"=>0));
    ?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-19
    • 2013-12-11
    • 2020-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-17
    • 1970-01-01
    相关资源
    最近更新 更多