【问题标题】:Chrome has "Failed to load PDF document" error message on inline PDFsChrome 在内嵌 PDF 上显示“加载 PDF 文档失败”错误消息
【发布时间】:2011-08-05 23:03:31
【问题描述】:

我在使用 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);

在这里,我将 Content-Disposition 设置为内联。因为如果用户浏览器有内置的 pdf 查看器插件,我想显示 pdf 文件。您可能知道,Chrome 有内置的 pdf 查看器。

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

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

【问题讨论】:

  • 改为强制下载(Content-disposition: attachment/Content-type: application/octet-stream),下载/保存好的PDF和坏的pdf,比较保存的和正在播放的服务器。
  • @Marc 我试试。它们都可以下载并且看起来一样。我还将它与服务器中的文件进行了比较。他们是一样的。当我切换回 Content-disposition: inline 时。它只是行不通。 :( 我什至比较了响应头。它们是一样的。
  • 如果您尝试直接查看而不是通过脚本会发生什么?
  • @Marc 您无需通过脚本即可直接查看。
  • 那么脚本的意义何在?

标签: php pdf google-chrome


【解决方案1】:

我一直在努力解决同样的问题。这与我在浏览器中获得一致的结果一样接近。我认为您可能遇到问题的原因是某些 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;

【讨论】:

  • 这不适用于任何浏览器还是仅适用于 Chrome?您可以尝试将缓冲区的大小从 1024 修改为更小的值。如果 $file 设置为文档的完整路径,这应该可以工作。另外,请注意其中一个标题中的 $myFileName 变量。这需要设置或删除。
  • 我似乎正在无限加载屏幕。
  • 对 ob_clean() 和 flush() 的良好调用
  • 这个解决方案对我来说效果很好,虽然我从数据库中获取 PDF 的内容,但我在将 PDF 输入数据库时​​使用了 file_get_contents() 从 PDF 中获取内容。跨度>
  • 我在这里遇到了问题。我的 pdf 的标题与我在标题中设置的不同 header("Content-Disposition: inline; filename=".$myFileName."");任何线索如何做到这一点?
【解决方案2】:

我有类似的问题,但我注意到订单很重要。看来; 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);

【讨论】:

  • 是的,从 finder 中打开下载,但 Chrome 下载栏(除非使用“系统查看器”)不会产生“加载 PDF 文档失败”。经过几十个示例包括此页面上的解决方案...这是停止 CHROME“错误无法加载 PDF 文档。重新加载”弹出窗口的示例。这是我最初的开始:header('HTTP/1.1 200 OK'); header('Content-type:application/pdf'); header('Content-Disposition:attachment; filename="'.basename($f).'"'); 并在 Firefox 中运行良好。
【解决方案3】:

我已经解决了这个问题

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

【讨论】:

    【解决方案4】:

    遇到同样的问题,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');
    

    【讨论】:

      【解决方案5】:

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

          header('HTTP/1.1 200 OK');
      

      【讨论】:

        【解决方案6】:

        在浪费了几个小时之后...我添加了 cmets 以指出 @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();
        

        在大约 30 分钟的时间里,我被各种变体愚弄了……但我可以将其归结为“添加 HTTP 200 ",而不是“添加字节”,而不是“引用您的文件名”,而不是“分隔文件结尾”。这些都不起作用。

        (再次感谢@Kal)。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-05-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-01-27
          • 2015-06-16
          • 2015-07-09
          • 2014-06-05
          相关资源
          最近更新 更多