php Chrome 在嵌入式 PDF 上显示“无法加载 PDF 文档”错误消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5670785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:09:06  来源:igfitidea点击:

Chrome has "Failed to load PDF document" error message on inline PDFs

phppdfgoogle-chrome

提问by easycoder

I have a problem with reading pdf file in Chrome by using PHP.

我在 Chrome 中使用 PHP 读取 pdf 文件时遇到问题。

The following code is how I do in PHP

以下代码是我在 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);

In here, I set the Content-Disposition to inline. Because I want to display the pdf file if user browser have build-in pdf viewer plugin. As you may know, Chrome has build-in pdf viewer.

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

The problem is I have bunch of pdf files on the server. Only some of them can be viewed by Chrome. I can't figure out why others can not work the same way. I have checked the permission of each files. It looks like not the permission problem.

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

Is there anyone know what the problem is? Thank you.

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

回答by mingala

I've been wrestling with this same issue. This is as close as I got to consistent results across browsers. I think that the reason you could be having problems is if some PDF's are too large for readfile() to handle correctly. Try this:

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

回答by Esteban Alarcon

i've fixed this way

我已经解决了这个问题

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

回答by xor

Had the same problem, chrome didn't display the inline PDF, stuck at loading. The solution was to add header('Accept-Ranges: bytes').

有同样的问题,chrome 没有显示内联 PDF,卡在加载中。解决方案是添加header('Accept-Ranges: bytes').

My complete code:

我的完整代码:

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

回答by modifiedcontent

For me adding the following header fixed this annoying Chrome bug (?):

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

    header('HTTP/1.1 200 OK');

回答by Kal

I had similar issue but I noticed the order matters. Seems that ; filename=must have quotes around it, Content-Disposition: attachmentTry this:

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