无法在浏览器dompdf中加载pdf

时间:2016-02-13 08:48:18

标签: php pdf dompdf

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

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

1 个答案:

答案 0 :(得分:2)

试试这个并从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));
    ?>