Php 铬有;未能加载PDF文档";内联PDF上的错误消息

Php 铬有;未能加载PDF文档";内联PDF上的错误消息,php,pdf,google-chrome,Php,Pdf,Google Chrome,我在使用PHP阅读Chrome中的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-che

我在使用PHP阅读Chrome中的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);
在这里,我将内容配置设置为inline。因为如果用户浏览器有内置的pdf查看器插件,我想显示pdf文件。您可能知道,Chrome内置了pdf查看器

问题是服务器上有很多pdf文件。Chrome只能查看其中的部分内容。我不明白为什么其他人不能以同样的方式工作。我已经检查了每个文件的权限。看起来不是权限问题


有人知道问题出在哪里吗?谢谢。

我一直在努力解决这个问题。这与我在不同浏览器中获得的一致结果非常接近。我认为出现问题的原因可能是某些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;
    $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);

我也有类似的问题,但我注意到了顺序问题。似乎是
;filename=
必须在其周围加引号,
内容处置:附件
尝试以下操作:

$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;
    $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);
我已经修好了

$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);

同样的问题,chrome没有显示内联PDF,在加载时卡住了。解决方案是添加
标题('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');

对于我来说,添加以下标题修复了这个恼人的Chrome错误(?):


在浪费了几个小时之后……我添加了一些评论,指出@Kal是唯一有效的解决方案。但无论如何,这还不够……当Chrome做到这一点时,这是一个不可能且令人沮丧的问题:

错误:无法加载PDF文档。重新装载

这是结束折磨的区别

-        // Waste time with chrome:
-        header("Content-type:application/pdf");
-        header("Content-Disposition:attachment;filename=$file_basename");
-        readfile($file);
         exit();
---------------------------

+        // Deliver the 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);
         exit();
大约有三十分钟,我愚弄了各种各样的内容……但我可以将其归结为“添加HTTP 200”,而不是“添加字节””,不是“引用您的文件名””,也不是“分隔文件结尾”。这些都不起作用


(再次感谢@Kal)。

更改为强制下载(内容配置:附件/内容类型:应用程序/八位字节流),下载/保存好的PDF和坏的PDF,并将保存的内容与服务器上的内容进行比较。@Marc我尝试了一下。它们都可以下载,看起来一样。我还将其与服务器中的文件进行比较。它们是一样的。当我切换回内容配置时:内联。它就是不起作用(我甚至比较了响应标题。它们是一样的。如果您尝试直接查看一个而不是查看脚本,会发生什么?@Marc您可以直接查看,而不必查看脚本。那么脚本的目的是什么?这对任何浏览器或Chrome都不起作用吗?您可以尝试从1024 t修改缓冲区的大小o更小的。如果$file设置为文档的完整路径,这应该可以。另外,请注意其中一个标题中的$myFileName变量。这需要设置或删除。我似乎得到了一个无限的加载屏幕。对ob_clean()和flush()的调用很好这个解决方案对我来说效果很好,虽然我是从数据库中获取PDF的内容,但我在将PDF输入数据库时使用了file_get_contents()从PDF中获取内容。我在这里遇到了一个问题。我的PDF的标题与我在标题标题中设置的标题不同(“内容配置:inline;filename=“.myFileName.”);有什么线索可以做到这一点吗?是的,下载会从finder打开,但Chrome下载栏(除非使用“系统查看器”)不会显示“加载PDF文档失败”。在本页上的几十个示例(包括解决方案)之后,这就是停止Chrome“加载PDF文档失败。重新加载”错误的原因弹出窗口。这是我最初开始使用的:
标题('HTTP/1.1 200 OK');标题('Content-type:application/pdf');标题('Content-Disposition:attachment;filename=“'.basename($f)。”);
在Firefox中运行良好。