php - PDF 文件未下载
问题描述
我想下载一个带有结果的 pdf 文件,我已经使用 fpdf [$pdf->Output($output_file, 'F');] 生成了 pdf,生成的 pdf 文件保存在服务器中。
无论如何,我希望用户能够下载从 fpdf 生成的 pdf 文件。我什至尝试使用其他输出(http://www.fpdf.org/en/doc/output.htm),但除了“F”之外,它们都不起作用。$pdf->Output($output_file, 'F');(使用此生成pdf成功)
我还尝试添加一个表单,它将现有的生成文件从服务器下载到浏览器,但没有任何效果。我的代码如下:
<form method="post" target="_blank">
<input type="submit" name="download" value="Download Most Recent PDF">
<h3>Please check the content of PDF.</h3>
</form>
$file = plugin_dir_path(__FILE__) . "../pdf/Result.pdf";
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
我想生成结果(我成功地做到了)并让用户下载 pdf 版本的结果。(我被卡住了)。
解决方案
首先检查您的文件路径是否正确。如果您的文件未下载,然后再进行调试,请尝试以下代码,
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private', false);
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="'. basename($filename) . '";');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($filename));
echo file_get_contents($filename);
exit;
推荐阅读
- c++ - 无法在 C/C++ Visual Studio 中使用宽字符串,即使它可以在使用 MinGW 的 CodeBlocks 中使用
- python - 从列表字典更新我的数据框
- php - 如何在 WampServer 中进行并发执行?
- r - 使用相同的 alpha/lambda 参数从 caret 和 cv.glmnet 获得不同的 AUC 估计值
- vue.js - 如何路由到子文件夹/子级别中存在的 vue 文件?
- c++ - C++:析构函数可以是类中唯一的虚函数吗?
- r - R新手......我在这个计算中做错了什么?
- azure - 训练自定义表单,selectionMark边界框识别问题
- list - 如何在控制台上向用户显示问题?
- mysql - 如何合并所有,但分离 mySQL 中的列