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

时间:2014-06-24 07:18:01

标签: php symfony pdf html2pdf

你怎么样? 好! 今天我想用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()函数时才有效,所以如果有人知道它的话,可以说话! :)

5 个答案:

答案 0 :(得分:3)

您可以使用以下方法......

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

答案 1 :(得分:0)

之前我使用过类似的库,tcpdf。

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

答案 2 :(得分:0)

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

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

关于exit,它可能来自您的ob_start()电话。此函数打开响应缓冲区,而不发送它。 html2pdf output函数不需要ob_start函数;)

答案 3 :(得分:0)

或者你可以试试这个

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

答案 4 :(得分:0)

在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();