chrome 浏览器加载 PDF 文档失败

新手上路,请多包涵

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

在 chrome 中生成 PDF 时出现以下错误。

生成 PDF 时在 chrome 浏览器中出现错误

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

原文由 Ketav 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 927
2 个回答

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

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

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

原文由 Parthiban K 发布,翻译遵循 CC BY-SA 3.0 许可协议

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

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

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

原文由 Sébastien Gicquel 发布,翻译遵循 CC BY-SA 3.0 许可协议

推荐问题