[英]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();
当您使用 html 到 PDF 库(例如 mPDF)时,也会发生这种情况,并且您在发送文件之前以某种方式将 HTML 发送到浏览器。 许多读者在阅读 PDF 标记之前会忽略 HTML——Chrome 不会。
例如,在 PHP 中,在将数据发送到 mPDF 之前清除输出缓冲区: ob_clean()
。
这可能是生成的 pdf 的问题。 如果它适用于 firefox,请下载文件并尝试打开它。 如果您电脑中的 pdf 查看器输出损坏的 pdf ,那么您可能需要调整您的代码。 我面临着同样的问题。 由于 pdf 损坏,Chrome 无法打开它。
希望我的回答能让你踏上调试之旅。 干杯。 :D
同意@Sébastien Gicquel,但是,对于我的情况,我需要将 flush+ob_clean 放在标头之后但在@readfile 之前。
仅供参考,我的 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);
您可能会发现 Chrome 的标头名称有问题: Content-Type
value: charset=utf-8
。 删除响应标头值为我修复了它。 (原帖)
这是人们在更旧版本的 Chrome 上遇到的问题。 如果您仍然看到此问题,请执行以下操作
在 Google Chrome 中,您有 2 个选项来查看 PDF 文件。 您可以使用 Chrome pdf 查看器(默认),也可以使用 Adobe Reader
您可以查看 chrome://plugins(在地址栏中输入)吗? 并通过启用它切换到其他 PDF 查看器(Chrome/Adobe)!
我也面临同样的“无法加载 PDF 文档”问题,但应用ob_clean();
在发送标头之前解决了错误,因为此函数丢弃了输出缓冲区的内容
就我而言,我发现在创建 PDF 之前我有一些 php 警告,这些警告导致 pdf 损坏,因此浏览器无法读取。 如果您想摆脱这个问题,请尝试使用以下代码检查warnings
:
error_reporting(E_ERROR | E_PARSE);
我是通过最初在LINK中发布的@Mr.Web 的回答了解到这一点的
以下代码块在 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();
在 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();
希望这是有帮助的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.