【发布时间】:2018-12-29 08:12:58
【问题描述】:
我想下载一个带有结果的 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 版本的结果。(我卡住的地方)。
【问题讨论】:
-
当我点击“下载最新的 PDF”时,浏览器会打开类似这样的内容,.....%PDF-1.4 3 0 obj endobj 4 0 obj 流 x�3R��2�35W(�*T0P�R0T(�Y@��� z��F�@���g �z���& ɹ �!> .� �\�iK endstream endobj 1 0 obj endobj 5 0 obj /Resources 6 0 R /Filter /FlateDecode /Length 99 >> 流 x-�; �0{O1v�h�I�x�Z 1��l�;0�����XE�:\c9f�!iBB�I��Te����p�|e=a����̿d�1hd��Kdc��n��g endstream endobj 6 0 obj /Font >> endobj 8 0 obj endobj 10 0 obj stream x]��n�D�|��C�-B�RE�MT��am! ���/7�z�3�`X~n�[
-
你为什么不在 $file 中给出文件路径?我觉得你的文件路径不对
-
"C:\xampp\htdocs\xx\wp-content\plugins\Etc\pdf\pdf\result.pdf" 这是路径...我的结果 pdf 是 result.pdf
-
$file = plugin_dir_path(FILE) 。 “../pdf/Result.pdf”;对于那条路..我可以这样写还是不写?
-
为什么使用 plugin_dir_path() ?