【问题标题】:php pdf file download : Failed to load PDF Documentphp pdf文件下载:无法加载PDF文档
【发布时间】:2015-09-07 05:49:43
【问题描述】:

我正在尝试使用 php 下载 pdf 文件。我可以下载文本文件和图像 我的 PHP 代码:

header("Content-Type: application/octet-stream");

$file = $_GET["file"] .".pdf";
header("Content-Disposition: attachment; filename=" . urlencode($file));    
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Type: application/pdf");   // pdf might change to another format eg. doc,docx
header("Content-Description: File Transfer");            
header("Content-Length: " . filesize($file));
flush(); // this doesn't really matter.
$fp = fopen($file, "r");
while (!feof($fp))
{
echo fread($fp, 65536);
flush(); // this is essential for large downloads
} 
fclose($fp); 

这里是 HTML

部分:

<a href="download.php?file=abc">Download CV</a>

在我的情况下,我可以下载 PDF 文件,它也显示内存大小,但是当我打开它时,会显示如下错误:

加载 PDF 文档失败

我没有收到任何错误,我尝试更改内存限制,但都无法正常工作。任何帮助将不胜感激。

编辑:

我需要下载所有文件格式,并在我的代码中进行一些简单的更改。 示例:

 header("Content-Type: application/pdf");   // pdf might change to another format eg. doc,docx 

我不想保存浏览器中显示的 PDF 文件,因为在其他格式的情况下,我无法直接从浏览器保存。

当我尝试从下载文件夹打开文件时,它显示错误 喜欢:

Adobe reader 无法打开文件名 abc.pdf,因为它不是受支持的文件类型或文件已损坏

【问题讨论】:

标签: php pdf download


【解决方案1】:

你可以试试这个吗?

改变你的

echo fread($fp, 65536);

echo fread($fp, filesize($file));

我的猜测是 pdf 文件的长度与您指定的不同。

【讨论】:

    猜你喜欢
    • 2019-05-21
    • 2018-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多