2011-04-14 677 views
6

我在使用PHP在Chrome浏览器中阅读PDF文件时遇到问题。Chrome在线PDF中存在“无法加载PDF文档”的错误消息

下面的代码是我在PHP

$path = "actually file path"; 
header("Pragma: public"); 
header("Expires: 0"); 
header("Content-type: $content_type"); 
header('Cache-Control: private', FALSE); 
header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
header("Content-Disposition: inline; filename=\"$filename\""); 
header('Content-Transfer-Encoding: binary'); 
header('Content-Length' . filesize($path)); 
ob_clean(); 
flush(); 
readfile($path); 

怎么办。在这里,我设置的内容配置内联。因为如果用户浏览器内置pdf查看器插件,我想显示pdf文件。您可能知道,Chrome内置pdf查看器。

问题是我有一堆的pdf文件在服务器上。 Chrome浏览器只能查看其中的一部分。我无法弄清楚为什么其他人不能以相同的方式工作。我已检查每个文件的权限。它看起来不是权限问题。

有没有人知道问题是什么?谢谢。

+2

更改为强迫下载(内容处置:附件/内容类型:应用程序/八位字节流),下载/保存好PDF和坏的PDF和比较保存到服务器上的内容。 – 2011-04-14 23:21:31

+0

@Marc我尝试。他们都可以下载并看起来像一样。我也将它与服务器中的文件进行比较。他们是一样的。当我切换回Content-disposition:inline。它只是不起作用。 (我甚至比较了响应标题,它们是一样的) – easycoder 2011-04-14 23:30:34

+0

如果你试图直接查看一个而不是通过你的脚本,会发生什么? – 2011-04-14 23:33:13

回答

10

我一直在摔跤这个相同的问题。这和我在浏览器中获得一致的结果一样接近。我认为你可能会遇到问题的原因是如果某些PDF太大而无法正确处理readfile()。试试这个:

$file = "path_to_file"; 
$fp = fopen($file, "r") ; 

header("Cache-Control: maxage=1"); 
header("Pragma: public"); 
header("Content-type: application/pdf"); 
header("Content-Disposition: inline; filename=".$myFileName.""); 
header("Content-Description: PHP Generated Data"); 
header("Content-Transfer-Encoding: binary"); 
header('Content-Length:' . filesize($file)); 
ob_clean(); 
flush(); 
while (!feof($fp)) { 
    $buff = fread($fp, 1024); 
    print $buff; 
} 
exit; 
+0

感谢mingala ..会试试看。 – easycoder 2011-04-18 17:34:33

+1

它似乎无法正常工作...... :( – easycoder 2011-04-18 17:52:19

+1

这对于任何浏览器或Chrome都不适用吗?您可以尝试将缓冲区的大小从1024更改为小一些如果$ file设置为文档的完整路径,这个解决方案需要设置或删除 – mingala 2011-04-26 17:46:41

0

我有类似的问题,但我注意到顺序很重要。似乎; filename=必须周围的引号,Content-Disposition: attachment试试这个:

$file = "/files/test.pdf"; 
    $finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension 
    $mime = finfo_file($finfo, $file); 

    header('Pragma: public'); 
    header('Expires: 0'); 
    header('Content-Type: $mime'); 
    header('Content-Description: File Transfer'); 
    header('Content-Disposition: attachment; filename="'.basename($file).'"')); 
    header('Content-Transfer-Encoding: binary'); 
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
    header('Content-Length' . filesize($file)); 
    ob_clean(); 
    flush(); 
    readfile($file); 
1

我已经解决这样

$path = 'path to PDF file'; 
header("Content-Length: " . filesize ($path)); 
header("Content-type: application/pdf"); 
header("Content-disposition: inline; filename=".basename($path)); 
header('Expires: 0'); 
header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
ob_clean(); 
flush(); 
readfile($path); 
1

有同样的问题,Chrome未显示在线PDF,停留在装载。解决方法是添加header('Accept-Ranges: bytes')

我的完整代码:

header('Content-Type: application/pdf'); 
header('Content-Disposition: inline; filename="'.$title.'"'); 
header('Content-Transfer-Encoding: binary'); 
header('Content-Length: '.filesize($file)); 
header('Accept-Ranges: bytes'); 
header('Expires: 0'); 
header('Cache-Control: public, must-revalidate, max-age=0');