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

问题描述 投票:0回答:1

我正在尝试使用 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个回答
0
投票

你能试试这个吗?

改变你的

echo fread($fp, 65536);

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

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

© www.soinside.com 2019 - 2024. All rights reserved.