【问题标题】:What is data as a "string without null bytes, not Binary"?什么是“没有空字节的字符串,而不是二进制”的数据?
【发布时间】:2015-05-13 10:44:44
【问题描述】:

我可以使用poppler library 轻松地从 pdf 创建图像:

pdftoppm -png myfile.pdf > myfile.png

我现在正在尝试使用python-poppler library 在 Python 中执行相同的操作。安装 lib (sudo apt-get install python-poppler) 后,我可以使用以下命令加载 pdf 文件:

doc = poppler.document_new_from_file('file://'+urllib(inputF), password=None)

但我现在想从二进制文件加载一个 pdf 文件。我以为我可以使用方法poppler.document_new_from_data(),所以我尝试了以下,它返回一个类型错误:

>>> d = poppler.document_new_from_data(userDoc.binary)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: document_new_from_data() argument 1 must be string without null bytes, not Binary

我不确定这意味着什么。什么“数据”可以是“没有空字节的字符串,而不是二进制”?我尝试检查该方法的源代码,但源文件 (here) 甚至不包含单个 .py 文件。

我尝试将二进制文件转换为 base64,但这会导致错误提示 TypeError: Required argument 'length' (pos 2) not found

欢迎任何帮助!

[编辑] 感谢@Vaulstein 的提示,我现在走得更远了:

s = binascii.a2b_base64(userDoc.binary)
r = poppler.document_new_from_data(s, len(s), password='')Syntax Warning: May not be a PDF file (continuing anyway)
Syntax Error (3): Illegal character <75> in hex string
Syntax Error (4): Illegal character <df> in hex string
Syntax Error (5): Illegal character <5d> in hex string
Syntax Error (6): Illegal character <28> in hex string
Syntax Error (7): Illegal character <6e> in hex string
Syntax Error (8): Illegal character <3f> in hex string
Syntax Error (9): Illegal character <ca> in hex string
Syntax Error (10): Illegal character <89> in hex string
Syntax Error (11): Illegal character <db> in hex string
>>> r = poppler.document_new_from_data(s, len(s), password='')
Traceback (most recent call last):
  File "<input>", line 1, in <module>
GError: PDF document is damaged

但它似乎仍然不是正确的编码。还有其他想法我该怎么做吗?

【问题讨论】:

  • 你试过binascii.a2b_base64(data)吗?
  • @Vaulstein - 我刚刚尝试过,这确实让我更进一步。我现在收到GError: PDF document is damaged。我在问题中添加了一个新部分。知道现在可能出了什么问题吗?
  • 你应该检查链接poppler
  • @Vaulstein - 谢谢你的链接。所以我现在明白问题是“这种PDF中的第一个对象是以“
  • 您可以在test1.textest2.tex 两个文档之间的链接上找到差异,这是添加虚拟对象的方式:\immediate\pdfobj stream {}

标签: python pdf typeerror poppler


【解决方案1】:

poppler_document_new_from_data 调用需要将整个二进制数据(包括 0 字节)作为第一个参数传递为 char*(在 Python 2 中通常是 str)。您在 poppler-python 中发现了一个错误。正如@Vaulstein 在评论中指出的那样,它是reported upstream but is unresolved

作为一种解决方法,将 PDF 存储到文件并使用 ..new_from_file 调用,或者改用 gi.repository.Popplermodule。 (该模块带有 PyGObject;例如,请参阅 herehere's the documentation for poppler_document_new_from_data。)

【讨论】:

  • @Philip:我认为该错误已经报告但尚未解决。它存在于错误列表中。
  • @Vaulstein 谢谢,我补充了。
猜你喜欢
  • 1970-01-01
  • 2016-02-08
  • 2023-02-15
  • 1970-01-01
  • 2020-03-06
  • 1970-01-01
  • 2015-01-15
  • 2012-12-29
  • 1970-01-01
相关资源
最近更新 更多