简体   繁体   English

比萨生成的pdf在Chrome中可见,但在Adobe Reader中不可见

[英]pisa generated pdf visible in Chrome but not in Adobe reader

I am trying to save a rendered html page in flask as pdf. 我试图将呈现的html页面保存在烧瓶中为pdf。 I tried the following things: 我尝试了以下操作:

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

and

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

but generated pdf file is perfectly visible in Google Chrome (opened directly by going to the location on disk), but i get blank page in Adobe acrobat reader. 但是生成的pdf文件在Google Chrome浏览器中完全可见(直接转到磁盘上的位置即可打开),但是在Adobe acrobat reader中却出现了空白页。

Similar problem that i can find: PDF text show in Google Chrome but not in Adobe Acrobat 我可以找到类似的问题: PDF文本显示在Google Chrome中,而不显示在Adobe Acrobat中

but i am not sure how to implement above solution in python Chrome Version 41.0.2272.118 m Adobe Reader XI 但是我不确定如何在python Chrome版本41.0.2272.118 m Adob​​e Reader XI中实现上述解决方案

I'm not a Python expert (I know the name, that's about it) so my solution may not be correct. 我不是Python专家(我知道名字,就是这样),所以我的解决方案可能不正确。 But I do think I know what the problem is and that should help you find the solution you're looking for. 但是我确实认为我知道问题出在哪里,这应该可以帮助您找到所需的解决方案。

When you open your file with a binary editor (or a good text editor showing invisible characters) you'll see that each line ending consists of three line-ending characters: 当使用二进制编辑器(或显示不可见字符的好的文本编辑器)打开文件时,您会看到每个行尾都由三个行尾字符组成:

x0D x0D x0A x0D x0D x0A

Or otherwise said 或以其他方式说

Carriage Return, Carriage Return, Line feed 回车,回车,换行

This is wrong. 错了 From the PDF specification: 根据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." “按照惯例,PDF文件中的标记按行排列;请参见7.2,“词汇惯例”。每行应以行尾(EOL)标记终止,该标记可以是回车符( 0Dh),行进给(0Ah)或两者兼而有之。具有二进制数据的PDF文件可能具有任意长的行。”

I think this is what breaks Adobe Reader. 我认为这是破坏Adobe Reader的原因。 It's strange that Adobe Reader and Acrobat throw a fit on this file, while many other (worse) PDF readers (such as Mac OS X Preview) seem to show it without any problem. 奇怪的是Adobe Reader和Acrobat对此文件进行了拟合,而许多其他(更差)的PDF阅读器(例如Mac OS X Preview)似乎都没有问题。

All of this said, you seem to have a problem with line endings. 所有这些都说明,您似乎对行尾有问题。 Given my limited knowledge of Python all I might point you to is the line: 鉴于我对Python的了解有限,我可能只想指出以下几点:

file(filename, "w")

I read in Python documentation that on some platforms this can treat files as ASCII files and ruin binary files. 我在Python文档中读到,在某些平台上,这可以将文件视为ASCII文件并破坏二进制文件。 As a PDF is definitely a binary file, I would change that to: 由于PDF绝对是二进制文件,因此我将其更改为:

file(filename, "wb")

and see what happens. 看看会发生什么。

I can tell you that for a far as I could see the rest of the file structure seems correct. 我可以告诉您,就我所知,其余文件结构似乎都是正确的。 So I think you have all necessary objects etc to show the file correct (as proven by Chrome and Mac Preview), so I really think the line ending problem is the one you need to solve. 因此,我认为您具有所有必要的对象等才能正确显示文件(已通过Chrome和Mac Preview证明),因此我真的认为行尾问题是您需要解决的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM