【问题标题】:PDF file isn't downloadingPDF 文件未下载
【发布时间】: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() ?

标签: php pdf download fpdf


【解决方案1】:

首先检查你的文件路径是否正确。如果你的文件没有下载,然后再调试,然后试试下面的代码,

    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:\xampp\htdocs\xx\wp-content\plugins\Etc\pdf\pdf\result.pdf"
  • result.pdf 是需要下载的 pdf...当我放 plugin_dir_path(FILE) 时。 “../pdf/Result.pdf”或“C:/xampp/htdocs/xx/wp-content/plugins/Etc/pdf/pdf/result.pdf”无论哪种方式都没有区别......它没有下载
  • 先用 echo $file 打印你的路径;并在 if (file_exists($file)) { echo "path exists";死;}
  • 好的,我知道您要下载哪个文件。您检查 $file 的路径了吗?和这个“C:\xampp\htdocs\xx\wp-content\plugins\Etc\pdf\pdf\result.pdf”路径一样吗?
猜你喜欢
  • 2019-01-22
  • 1970-01-01
  • 2019-06-28
  • 1970-01-01
  • 2017-04-17
  • 2012-11-14
  • 1970-01-01
  • 2021-09-09
相关资源
最近更新 更多