php pdf文件下载:无法加载PDF文档

时间:2015-09-07 05:49:44

标签: php pdf download

我正在尝试使用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文件,因为在其他格式的情况下我无法直接从浏览器中保存。

  

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

Adob​​e阅读器无法打开文件名abc.pdf,因为它不是受支持的文件类型,或者因为文件已损坏

1 个答案:

答案 0 :(得分:0)

你能试试吗?

更改您的

echo fread($fp, 65536);

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

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

相关问题