首页 > 解决方案 > 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 版本的结果。(我被卡住了)。

标签: phppdfdownloadfpdf

解决方案


首先检查您的文件路径是否正确。如果您的文件未下载,然后再进行调试,请尝试以下代码,

    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;

推荐阅读