php Chrome 中 ERR_INVALID_RESPONSE 中的 PDF 生成结果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34599266/
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-26 00:07:10  来源:igfitidea点击:

PDF Generation Results in ERR_INVALID_RESPONSE in Chrome

phpgoogle-chromepdf

提问by Andrew Klatzke

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.

在浏览器中以编程方式(通过 PHP)生成 PDF 时,渲染的 PDF 在 Firefox 和 Safari 中显示良好,但 Chrome 返回 ERR_INVALID_RESPONSE。它是一个有效的 PDF - 一旦从工作浏览器中保存,就可以使用 Adob​​e Reader/Preview 在本地打开,甚至可以在从不同浏览器保存 PDF 后在 Chrome 中打开。

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.

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

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

我对它进行了研究,但只能找到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):

我有一个暗示,这是一个标题错误。生成 PDF 后,以下标题将发送到浏览器(在 FF、Safari 和 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:

在 Stack Overflow 上搜索后,我也尝试添加以下标题,但无济于事:

    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?

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

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.

编辑:我更突出的问题之一是什么可能导致它在 Chrome 本地正常工作,但不能在服务器环境中工作。

采纳答案by Andrew Klatzke

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.

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

It turns out that mod_secon 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_RESPONSErather than a 403.

事实证明,mod_sec我们的服务器将请求(出于某种原因仅来自 Chrome)标记为尝试进行文件注入攻击,并返回 403 禁止响应。Chrome 将此显示为ERR_INVALID_RESPONSE403 而不是 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.

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

回答by Cyberdelphos

In my case I had to add these 2 parameters to headersbecause wordpress was sending 404 code as it didn't recognize the url of my php function:

就我而言,我必须将这 2 个参数添加到标题中,因为 wordpress 发送 404 代码,因为它无法识别我的 php 函数的 url:

header("Content-type: application/pdf",true,200);

as stated in this answer on wordpress.stackexchange.

wordpress.stackexchange 上的这个答案所述。

This forces the headers to replace (2nd param true) the 404 status code generated by wordpress as it does not recognize the custom url, and sets 200 OK (3rd param 200).

这会强制标头替换(第二个参数true)由 wordpress 生成的 404 状态代码,因为它无法识别自定义 url,并设置 200 OK(第三个参数200)。

So it ended being something like this:

所以它最终是这样的:

$pdf_name = "test.pdf";
$pdf_file = "/absolute/path/to/my/pdfs/on/my/server/{$pdf_name}";
header('Content-type: application/pdf',true,200);
header("Content-Disposition: attachment; filename={$pdf_name}");
header('Cache-Control: public');
readfile($pdf_file);
exit();

回答by Alpesh Panchal

Try this

尝试这个

<?php
$filename = 'Physical Path to PDf file.pdf';
$content = file_get_contents($filename);

header("Content-type:application/pdf");

// It will be called downloaded.pdf
header("Content-Disposition:inline;filename='".basename($filename)."'");   
header('Content-Length: '.strlen( $content ));

// The PDF source is in original.pdf
readfile($filename);
?>

<html>
<body>
...
...
...

Make sure that above header code is called before output of PHP script is sent to browser.

确保在 PHP 脚本的输出发送到浏览器之前调用了上面的头代码。