pisa 生成的 pdf 在 Chrome 中可见,但在 Adob​​e reader 中不可见

pisa generated pdf visible in Chrome but not in Adobe reader

我正在尝试将 Flask 中呈现的 html 页面保存为 pdf。 我尝试了以下操作:

pdf=render_template('exp1_post.html',some_data=some_data)
filename = "simplePrint.pdf"        
pisa.CreatePDF(pdf, file(filename, "w"))

pdf=render_template('exp1_post.html',some_data=some_data)
filename = "simplePrint.pdf"        
pisa.CreatePDF(pdf.encode("ISO-8859-1"), file(filename, "w"))

但生成的 pdf 文件在 Google Chrome 中完全可见(通过转到磁盘上的位置直接打开),但我在 Adob​​e acrobat reader 中得到空白页。

我能找到的类似问题:PDF text show in Google Chrome but not in Adobe Acrobat

但我不确定如何在 python 中实施上述解决方案 Chrome 版本 41.0.2272.118 m Adobe Reader十一

我不是 Python 专家(我知道名字,仅此而已)所以我的解决方案可能不正确。但我确实认为我知道问题出在哪里,这应该可以帮助您找到您正在寻找的解决方案。

当您使用二进制编辑器(或显示不可见字符的优秀文本编辑器)打开文件时,您会看到每一行结尾都由三个行结尾字符组成:

x0D x0D x0A

或另有说法

Carriage Return, Carriage Return, Line feed

这是错误的。来自 PDF 规范:

"As a matter of convention, the tokens in a PDF file are arranged into lines; see 7.2, "Lexical Conventions."Each line shall be terminated by an end-of-line (EOL) marker, which may be a CARRIAGE RETURN (0Dh), a LINE FEED (0Ah), or both. PDF files with binary data may have arbitrarily long lines."

我认为这就是破坏 Adob​​e Reader 的原因。奇怪的是,Adobe Reader 和 Acrobat 对此文件进行了调整,而许多其他(更差的)PDF 阅读器(例如 Mac OS X Preview)似乎可以毫无问题地显示它。

综上所述,您似乎对行结尾有疑问。鉴于我对 Python 的了解有限,我可能会向您指出以下行:

file(filename, "w")

我在 Python 文档中读到,在某些平台上,这会将文件视为 ASCII 文件并破坏二进制文件。由于 PDF 绝对是二进制文件,因此我将其更改为:

file(filename, "wb")

看看会发生什么。

我可以告诉你,据我所知,文件结构的其余部分似乎是正确的。所以我认为您拥有所有必要的对象等来正确显示文件(由 Chrome 和 Mac 预览证明),所以我真的认为行尾问题是您需要解决的问题。