保存并下载PDF

时间:2011-10-12 16:30:53

标签: php dompdf

我需要一起做两件事&与DOMPDF同时使用。

我需要一起做以下事情 - 这可能吗?

//print the pdf file to the screen for saving
$dompdf->stream("pdf_filename_".rand(10,1000).".pdf", array("Attachment" => false));
//save the pdf file on the server
file_put_contents('/home/stsnew/public_html/pdf/file.pdf', $dompdf->output()); 

如果单独/单独完成$dompdf->stream$dompdf->output(),上述工作正常,但当我尝试如上所示一起运行它们时,它就会崩溃。

非常感谢任何帮助/建议。

2 个答案:

答案 0 :(得分:22)

为什么不交换它们将pdf首先创建为文件,然后将创建的文件流回来?

$file_to_save = '/home/stsnew/public_html/pdf/file.pdf';
//save the pdf file on the server
file_put_contents($file_to_save, $dompdf->output()); 
//print the pdf file to the screen for saving
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="file.pdf"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file_to_save));
header('Accept-Ranges: bytes');
readfile($file_to_save);

答案 1 :(得分:1)

迟到但可能会有所帮助。

设置附件为true或1

$dompdf->stream("pdf_filename_".rand(10,1000).".pdf", array("Attachment" => true));

如果您只是想下载文件,则不需要file_put_contents()

相关问题