Chrome有“无法加载PDF文档”内联PDF中的错误消息 [英] Chrome has "Failed to load PDF document" error message on inline PDFs

查看:7784
本文介绍了Chrome有“无法加载PDF文档”内联PDF中的错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



以下代码是我如何在PHP中完成的工作

  $ 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浏览器只能查看其中的一部分。我无法弄清楚为什么其他人不能以相同的方式工作。我已检查每个文件的权限。它看起来不是权限问题。

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

解决方案

我一直在摔跤这个同样的问题。这和我在浏览器中获得一致的结果一样接近。我认为你可能会遇到问题的原因是如果某些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);
头(Content-Transfer-Encoding:binary);
header('Content-Length:'。filesize($ file));
ob_clean();
flush();
while(!feof($ fp)){
$ buff = fread($ fp,1024);
print $ buff;
}
exit;


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

The following code is how I do in 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.

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.

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

解决方案

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:

$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有“无法加载PDF文档”内联PDF中的错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆