php - 保存并下载 PDF

标签 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() 单独/单独完成,上面的工作正常,但是当我尝试将它们一起运行时如上所示,它只是崩溃。

如有任何帮助/建议,我们将不胜感激。

最佳答案

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

关于php - 保存并下载 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7743370/

相关文章:

php - 如何从 MySQL 数据库检索 BLOB 格式的图像并在 html <img> 标签中显示?

javascript - JCrop 和 Ajax 图像上传和裁剪(laravel 5、Jquery)

php - 如何将使用 DOM 抓取的链接插入 MySQL 数据库? (或者我做错了什么?)

php - DomPDF 不能很好地呈现表格

php - DOMPDF,表格单元格垂直对齐问题

javascript - 使用 hyphenator.js 在 dompdf 中进行连字符

php - Mysql查询连接表并仅选择不为空且具有相同ID的行

php - 我正在努力做,_POST 是空的

php - 如何使用dompdf打印页码并将页面边框设置为pdf

php - DOMPDF - 在个案基础上定义 DOMPDF_DPI?