我们正尝试使用 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);}
请告诉我们第二个代码有什么问题?
试试这个,从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));
?>