php 无法在 Chrome 浏览器中加载 PDF 文档
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34737498/
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
Failed to load PDF document in chrome browser
提问by Ketav Chotaliya
I'm using mPDF library for generating PDF files from HTML page. It is working nice in firefox but it is not display PDF file in chrome browser.
我正在使用 mPDF 库从 HTML 页面生成 PDF 文件。它在 Firefox 中运行良好,但在 chrome 浏览器中不显示 PDF 文件。
I'm getting following error while generate PDF in chrome.
在 chrome 中生成 PDF 时出现以下错误。
Following is my code for generate PDF using mPDF
以下是我使用 mPDF 生成 PDF 的代码
ob_clean();
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $yourFileName . '"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
$mpdf = new PDF( 'c','A4','','',15, 15,10,14,0,0);
$mpdf->useOnlyCoreFonts = false;
$mpdf->SetDisplayMode('real');
$mpdf->list_indent_first_level = 0; // 1 or 0 - whether to indent the first level of a list
$stylesheet = file_get_contents(APPPATH . 'third_party/mpdf/style.css');
$mpdf->WriteHTML($stylesheet,1);
$mpdf->WriteHTML($html);
$mpdf->Output();
采纳答案by Parthiban K
This is an issue people were running into on a much older version of Chrome. If you are still seeing this issue, do the following
这是人们在旧版 Chrome 上遇到的问题。如果您仍然看到此问题,请执行以下操作
In Google Chrome, you have 2 options to view PDF files. You can either use Chrome pdf viewer (default) or you can use Adobe Reader
在 Google Chrome 中,您有 2 个选项可以查看 PDF 文件。您可以使用 Chrome pdf 查看器(默认),也可以使用 Adobe Reader
Can you check chrome://plugins (type it in address bar) ? and switch to other PDF viewer (Chrome/Adobe) by just enabling it !
您可以检查 chrome://plugins (在地址栏中输入)吗?只需启用即可切换到其他 PDF 查看器(Chrome/Adobe)!
回答by Hamad Rakshani
This maybe is the problem with generated pdf. If it works on firefox, download the file and try to open it. And if pdf viewer in you pc outputs corrupted pdf, then you might need to tweak your code. I am facing the same problem. Chrome won't open it because of the corrupted pdf.
这可能是生成的 pdf 的问题。如果它适用于 Firefox,请下载该文件并尝试打开它。如果您电脑中的 pdf 查看器输出损坏的 pdf,那么您可能需要调整您的代码。我面临同样的问题。由于 pdf 损坏,Chrome 无法打开它。
Hope my answer will let you go to a journey of debugging. Cheers. :D
希望我的回答能让你踏上调试之旅。干杯。:D
回答by Sébastien Gicquel
In my case, the html of the current page was sent in the pdf (i see it when i open the pdf with a simple text editor).
就我而言,当前页面的 html 以 pdf 格式发送(当我使用简单的文本编辑器打开 pdf 时看到它)。
Solution for me flush+ ob_cleanbefore sending header
在发送标头之前为我刷新+ ob_clean 的解决方案
ob_clean();
flush();
header("Content-type:application/pdf");
header("Content-Disposition:attachment;filename='downloaded.pdf'");
echo $result;
exit;
回答by jorisw
This also happens when you're using an html to PDF library such as mPDF, and somehow you're sending HTML to the browser before sending the file. Many readers ignore the HTML before reading the PDF markup - Chrome does not.
当您使用 html 到 PDF 库(例如 mPDF)时也会发生这种情况,并且在发送文件之前以某种方式将 HTML 发送到浏览器。许多读者在阅读 PDF 标记之前会忽略 HTML - Chrome 不会。
For instance, in PHP, clear your output buffer before sending the data to mPDF: ob_clean()
.
例如,在PHP,将数据发送到MPDF之前清除输出缓冲区:ob_clean()
。
回答by Askeroglu
The following code block did the work for me in C# for the case of Chrome in browser pdf opening with MemoryStream:
下面的代码块在 C# 中为我完成了 Chrome 在浏览器 pdf 中使用 MemoryStream 打开的情况:
MemoryStream ms;
ms = new MemoryStream(result.ResponseData[0].Report);
HttpContext context = HttpContext.Current;
HttpContext context = HttpContext.Current;
context.Response.Clear();
context.Response.ClearContent();
context.Response.ClearHeaders();
context.Response.ContentType = "application/pdf";
context.Response.AddHeader("Content-Disposition", "inline;filename=" + Guid.NewGuid().ToString() + "." + _exportType);
context.Response.AddHeader("Content-Length", ms.Length.ToString());
context.Response.BinaryWrite(ms.ToArray());
context.Response.Flush();
context.Response.Close();
context.Response.End();
回答by Raju Paladiya
In Asp.net Core 2.10, this is worked fine for me.
在 Asp.net Core 2.10 中,这对我来说很好用。
MemoryStream ms1;
ms1 = new MemoryStream(res);
HttpContext context = _httpContextAccessor.HttpContext;
context.Response.Clear();
context.Response.ContentType = "application/pdf";
context.Response.Headers.Add("Content-Disposition", "inline;filename=" + Guid.NewGuid().ToString() + "." + "pdf");
context.Response.Headers.Add("Content-Length", ms1.Length.ToString());
context.Response.Body.Write(ms1.ToArray());
context.Response.Body.Flush();
context.Response.Body.Close();
Hope this is helpfull.
希望这有帮助。
回答by MeSo2
回答by Shiyu Fang
Agree with @Sébastien Gicquel, however, for my case, I need to put flush+ob_clean right after header but before @readfile.
同意@Sébastien Gicquel,但是,就我而言,我需要在标题之后但在@readfile 之前放置flush+ob_clean。
FYI,my Chrome version is 9.0.3945.79, and the code works well without ob_clean and flush in Firefox and IE.
仅供参考,我的 Chrome 版本是 9.0.3945.79,代码在 Firefox 和 IE 中没有 ob_clean 和flush 的情况下运行良好。
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' .$file. '"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
ob_clean();
flush();
@readfile($file);