php 保存并下载 PDF

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7743370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:17:48  来源:igfitidea点击:

Save and download PDF

phpdompdf

提问by user991830

I need to do 2 things together & at the same time with DOMPDF.

我需要用 DOMPDF 同时做两件事。

I need to do the following together - is this possible?

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

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

The above works fine if $dompdf->streamand $dompdf->output()are done separately/individually , but when I try to run them together as shown above, it just crashes.

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

Any help/advice would be really appreciated.

任何帮助/建议将不胜感激。

回答by Manse

Why dont you swap them round create the pdf first as a file and then stream the created file back ?

为什么不交换它们,首先将 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);

回答by Farhan

Answering late but might be will help onward.

回答晚了,但可能会有所帮助。

on set attachment true or 1

设置附件 true 或 1

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

No need of file_put_contents() if you are just looking for download

如果您只是在寻找下载,则不需要 file_put_contents()