使用 Pisa / xhtml2pdf 在 Python 中创建 pdf
我知道有很多基于 Python 中 pdf 创建的问题,但我还没有看到任何基于使用 Pisa 或 xhtml2pdf 创建 pdf 的问题。
这是我的代码。
pisa.pisaDocument(cStringIO.StringIO(a).encode('utf-8'),file('mypdf.pdf','wb'))
然后
pisa.startViewer('mypdf.pdf')
我通过几个不同的教程和示例将其组装起来,但我尝试过的每件事总是会导致 pdf 被损坏,并且在尝试打开 pdf 时收到此消息。
“Adobe Reader 无法打开‘awesomer.pdf’,因为它不是受支持的文件类型,或者文件已损坏(例如,它作为电子邮件附件发送且未正确解码)。”
即使我不在字符串上使用 .encode('utf-8') ,也会出现此消息。
我做错了什么?我的 Mac 上的编码与此有关吗?
I know there are a lot of questions based on pdf creation in Python but I haven't seen anything based on creating pdfs with Pisa or xhtml2pdf.
Here is my code.
pisa.pisaDocument(cStringIO.StringIO(a).encode('utf-8'),file('mypdf.pdf','wb'))
and then
pisa.startViewer('mypdf.pdf')
I assembled this over a couple different tutorials and examples but every single thing that I've tried always results in the pdf being corrupted and I get this message when trying to open the pdf.
"Adobe Reader could not open 'awesomer.pdf' because it is either not a supported file type or because the file has been damaged (for example, it was sent as an email attachment and wasn't correctly decoded)."
This message occurs even when I don't use the .encode('utf-8') on the string.
What am I doing wrong? Does the encoding on my Mac have to do with this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议手动关闭文件,也有类似的问题。试试这个:
I'd suggest closing the file manually, had a simmilar problem. Try this:
我建议执行以下操作:
然后查看错误输出是什么。
我不确定您正在编码什么字符串,但这也可能有帮助:
这取决于
a
是否需要进行 html 编码I recommend doing the following:
And then see what the error output is.
I'm not sure what string you are encoding but this might also help:
It depends on if
a
needs to be html encoded