Html2pdf:如何显示PDF(允许用户保存)并将其保存在服务器上


html2pdf: how to show the pdf (to allow the user to save it) and save it on the server

你好吗?好!今天我希望使用html2pdf。它工作得很好,我可以显示/保存pdf,但我不能两者兼得!这是个问题,我不知道该怎么办。我正在使用symfony库,下面的代码可以工作:

ob_start();
include(dirname(__FILE__).'/feuille.php');
$content = ob_get_clean();
$html2pdf = new 'Html2Pdf_Html2Pdf('P','A4','fr');
$html2pdf->pdf->SetDisplayMode('real');
$html2pdf->WriteHTML($content);
// $result = $html2pdf->Output($_POST["fournisseur"].".pdf", 'F'); //save on server
$result = $html2pdf->Output($_POST["fournisseur"].".pdf"); //show the page on browser
exit();

注释的代码只有在我注释后面的代码时才有效…那么,你有什么办法能让这两条线同时工作吗?

谢谢你的帮助!

PS:它也只工作,如果我在最后添加exit()函数,所以如果有人知道一些关于它,可能他说话!:)

您可以使用以下方法…

$result = $html2pdf->Output($_POST["fournisseur"].".pdf", 'F');
header("Content-type:application/pdf");
echo file_get_contents($_POST["fournisseur"].".pdf");

我以前使用过类似的库tcpdf。

我只是把文件保存在服务器,之后回显一个链接到浏览器或重定向用户与php。

如果您使用不带参数的output函数,您将能够看到PDF,而无需保存它。

您可以添加第二个功能$html2pdf->Output($_POST["fournisseur"].".pdf", 'F')以将PDF保存在磁盘上。

关于exit,它可能来自您的ob_start()呼叫。这个函数打开一个响应缓冲区,但不发送它。html2pdf output函数不需要ob_start函数;)

或者你可以试试这个

$html2pdf = new HTML2PDF('P','A4','fr');
$html2pdf->WriteHTML($content);
$html2pdf->Output('example.pdf');

在symfony2中,我们使用这样的代码来显示pdf使用

return new Response(file_get_contents($fileData['path']), 200, array('Content-Type' => 'application/pdf'));

下载文件

$response = new Response(file_get_contents($path));
$response->setStatusCode(200);
$response->headers->set('Content-Type', mime_content_type($path));
$response->headers->set(
    'Set-Cookie',
    'fileDownload=true; path=/');
$response->headers->set(
    'Content-Disposition',
    sprintf('attachment;filename="%s"', utf8_decode($name))
);
$response->send();