Chrome中的PDF生成结果在ERR_INVALID_RESPONSE中 [英] PDF Generation Results in ERR_INVALID_RESPONSE in Chrome

查看:1583
本文介绍了Chrome中的PDF生成结果在ERR_INVALID_RESPONSE中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当以编程方式在浏览器中生成PDF时(通过PHP),呈现的PDF在Firefox和Safari中均可正常显示,但Chrome会返回ERR_INVALID_RESPONSE。这是一个有效的PDF - 可以使用Adobe Reader / Preview在本地打开,一旦从工作的浏览器中保存,甚至在从另一个浏览器保存PDF时,甚至可以在Chrome中打开。



正在通过 file_get_contents()读取PDF文件,给出当前时间戳,然后传递给浏览器。解决方法包括将文件保存到临时位置并重定向用户(至少对于Chrome来说),但这并不理想。



我已经研究过它能够找到 2008年以来的错误报告

我有一个错误,它是一个标题错误。生成PDF后,下列标题被发送到浏览器(在FF,Safari和IE中再次正常工作):

 报头( '内容类型:应用/ PDF'); 
header(HTTP / 1.1 200 OK);

我也在堆栈溢出搜索后尝试添加以下头文件,但无济于事:

  header(Content-Transfer-Encoding:binary); 
头('Accept-Ranges:bytes');

是否缺少Chrome需要的标头?有没有人有获得动态生成的PDF显示在Chrome中的经验?

编辑:我的一个比较突出的问题是可能导致Chrome在Chrome中本地工作,但不能在服务器环境中工作。



预先感谢您提供任何帮助。 他们的答案。



事实证明,这与标题无关。尝试以各种方式更改/删除标题(检测编码,尝试使用或不使用内容长度等)之后,我们决定深入研究更深的httpd日志,以查看是否有任何问题针对Chrome解决。



事实证明,我们的服务器上的 mod_sec 标记了请求(仅出于某种原因从Chrome浏览器),作为对文件注入攻击的尝试并正在返回403禁止的回应。 Chrome将它显示为 ERR_INVALID_RESPONSE ,而不是403.



CDN的主机名存在于请求中我们在端点上进行了充分的检查,以确保该文件确实是一个允许的资源),而不是在服务器上构建URL。

When generating a PDF in the browser programmatically (via PHP) the rendered PDF displays fine in both Firefox and Safari, but Chrome returns an ERR_INVALID_RESPONSE. It is a valid PDF - can be opened locally with Adobe Reader/Preview once saved from the working browsers, and will even open in Chrome once the PDF is saved from a different browser.

The PDF file is being read through file_get_contents(), is given a current timestamp and then passed to the browser. A workaround would involve saving the file to a temporary spot and redirecting the user (for Chrome, at least) but this is not ideal.

I've researched it and only been able to find bug reports dating from 2008.

I have an inkling it's a header error. After the PDF is generated, the following headers are sent to the browser (again working fine in FF, Safari and IE):

    header('Content-type:application/pdf');
    header("HTTP/1.1 200 OK");

I've also tried adding the following headers after searching on Stack Overflow, but to no avail:

    header("Content-Transfer-Encoding: binary");
    header('Accept-Ranges: bytes');

Are there missing headers that Chrome requires? Does anyone have experience with getting dynamically generated PDFs to display in Chrome?

EDIT: One of my more salient questions is what could be causing this to work fine locally in Chrome, but wouldn't work on a server environment.

Thank you in advance for any help.

解决方案

I want to thank everyone for their answers.

It turns out this was not related to the headers. After attempting to change/remove headers in various ways (detecting encoding, trying with and without content-length, etc.) we decided to dig into the deeper httpd logs to see if anything was resolving differently for Chrome.

It turns out that mod_sec on our server was flagging the request (only from Chrome for some reason) as an attempt at a file injection attack and was returning a 403 forbidden response. Chrome displayed this as the ERR_INVALID_RESPONSE rather than a 403.

The hostname of the CDN was present in the request (we had ample checking at the endpoint to ensure that the file was indeed an allowed resource), and instead are building the URL out on the server instead.

这篇关于Chrome中的PDF生成结果在ERR_INVALID_RESPONSE中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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