Unicode错误PyPdf

Unicode error PyPdf

我尝试使用 requests 库下载多个 pdf,然后使用 pypdf 将它们合并在一起。一般来说,这工作正常,但对于某些 pdf,我只是收到一个错误。

MWE.py

import requests
from pyPdf import PdfFileWriter, PdfFileReader
from StringIO import StringIO


input = PdfFileReader(StringIO(response.content))
input.decrypt("")
output = PdfFileWriter()
output.addPage(input.getPage(0))

outputStream = file("document-output.pdf", "wb")
output.write(outputStream)
outputStream.close()

session.close()

错误

Traceback (most recent call last):
  File "mwe.py", line 21, in <module>
    input.decrypt("")
  File "/usr/local/lib/python2.7/dist-packages/pyPdf/pdf.py", line 894, in decrypt
    return self._decrypt(password)
  File "/usr/local/lib/python2.7/dist-packages/pyPdf/pdf.py", line 904, in _decrypt
    user_password, key = self._authenticateUserPassword(password)
  File "/usr/local/lib/python2.7/dist-packages/pyPdf/pdf.py", line 945, in _authenticateUserPassword
    encrypt.get("/EncryptMetadata", BooleanObject(False)).getObject())
  File "/usr/local/lib/python2.7/dist-packages/pyPdf/pdf.py", line 1818, in _alg35
    key = _alg32(password, rev, keylen, owner_entry, p_entry, id1_entry)
  File "/usr/local/lib/python2.7/dist-packages/pyPdf/pdf.py", line 1729, in _alg32
    m.update(id1_entry)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)

对于跟踪,我从文件中读取了输入,但我认为在这种情况下这并不重要。

我发现了一些与此问题相关的问题,但我无法解决我的具体问题。

好的,我发现这似乎是 pyPdf (1.13) 中的一个错误 https://github.com/mstamy2/PyPDF2/issues/51

到目前为止,使用 PyPDF2 (1.26.0) 可以正常工作。