无法在 chrome 浏览器中加载 PDF 文档

Failed to load PDF document in chrome browser

我正在使用 mPDF 库从 HTML 页面生成 PDF 文件。它在 firefox 中运行良好,但在 chrome 浏览器中无法显示 PDF 文件。

我在 chrome 中生成 PDF 时遇到以下错误。

以下是我使用 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();

这是人们 运行 在更旧版本的 Chrome 上遇到的问题。如果您仍然遇到此问题,请执行以下操作

在 Google Chrome 中,您有 2 个选项可以查看 PDF 文件。您可以使用 Chrome pdf 查看器(默认),也可以使用 Adob​​e Reader

你能查看 chrome://plugins(在地址栏中输入)吗?并通过启用它切换到其他 PDF 查看器 (Chrome/Adobe)!

这可能是生成的 pdf 的问题。 如果它适用于 firefox,请下载文件并尝试打开它。如果您电脑中的 pdf 查看器输出 损坏的 pdf,那么您可能需要调整您的代码。我面临着同样的问题。 Chrome 由于 pdf 已损坏,无法打开它。

希望我的回答能让你踏上调试之旅。 干杯。 :D

当您使用 html 到 PDF 库(如 mPDF)时也会发生这种情况,并且您在发送文件之前以某种方式将 HTML 发送到浏览器。许多读者在阅读 PDF 标记之前会忽略 HTML - Chrome 不会。

例如,在 PHP 中,在将数据发送到 mPDF 之前清除输出缓冲区:ob_clean().

在我的例子中,当前页面的 html 是在 pdf 中发送的(我在使用简单的文本编辑器打开 pdf 时看到了它)。

发送前 flush + ob_clean 我的解决方案 header

ob_clean();
flush();
header("Content-type:application/pdf");
header("Content-Disposition:attachment;filename='downloaded.pdf'"); 
echo $result; 
exit;

以下代码块在 C# 中为我完成了 Chrome 在使用 MemoryStream 打开的浏览器 pdf 中的情况:

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

在 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();

希望对您有所帮助。

您可能会发现 Chrome 名称的 header 有问题:Content-Type 值:charset=utf-8。删除响应 header 值为我修复了它。 (原文post

同意@Sébastien Gicquel 的观点,但是,对于我的情况,我需要在 header 之后但在@readfile 之前放置 flush+ob_clean。

仅供参考,我的 Chrome 版本是 9.0.3945.79,代码在没有 ob_clean 的情况下运行良好,并且在 Firefox 和 IE 中刷新。

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

我也遇到了同样的“无法加载 PDF 文档”问题,但正在申请 ob_clean(); 在发送 header 之前解决了错误 因为此函数丢弃输出缓冲区的内容