diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ed10eb..3f1e1c3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Support for Paeth PNG filter compression (predictor value = 4) ([#537](https://github.com/pdfminer/pdfminer.six/pull/537)) ### Fixed +- `KeyError` when `'Encrypt'` but not `'ID'` present in `trailer` ([#594](https://github.com/pdfminer/pdfminer.six/pull/594)) - Fix issue of ValueError and KeyError rasied in PDFdocument and PDFparser ([#573](https://github.com/pdfminer/pdfminer.six/pull/574)) - Fix issue of TypeError: cannot unpack non-iterable PDFObjRef object, when unpacking the value of 'DW2' ([#529](https://github.com/pdfminer/pdfminer.six/pull/529)) - `PermissionError` when creating temporary filepaths on windows when running tests ([#469](https://github.com/pdfminer/pdfminer.six/issues/469)) diff --git a/pdfminer/pdfdocument.py b/pdfminer/pdfdocument.py index 2da93c8..8233281 100644 --- a/pdfminer/pdfdocument.py +++ b/pdfminer/pdfdocument.py @@ -583,7 +583,14 @@ class PDFDocument: continue # If there's an encryption info, remember it. if 'Encrypt' in trailer: - self.encryption = (list_value(trailer['ID']), + if 'ID' in trailer: + id_value = list_value(trailer['ID']) + else: + # Some documents may not have a /ID, use two empty + # byte strings instead. Solves + # https://github.com/pdfminer/pdfminer.six/issues/594 + id_value = (b'', b'') + self.encryption = (id_value, dict_value(trailer['Encrypt'])) self._initialize_password(password) if 'Info' in trailer: diff --git a/samples/encryption/encrypted_doc_no_id.pdf b/samples/encryption/encrypted_doc_no_id.pdf new file mode 100644 index 0000000..bdab7dc Binary files /dev/null and b/samples/encryption/encrypted_doc_no_id.pdf differ diff --git a/tests/test_pdfdocument.py b/tests/test_pdfdocument.py index a6ce45b..3947b1b 100644 --- a/tests/test_pdfdocument.py +++ b/tests/test_pdfdocument.py @@ -1,4 +1,4 @@ -from nose.tools import raises +from nose.tools import assert_equal, raises from helpers import absolute_sample_path from pdfminer.pdfdocument import PDFDocument @@ -14,3 +14,14 @@ class TestPdfDocument(object): parser = PDFParser(in_file) doc = PDFDocument(parser) doc.getobj(0) + + def test_encrypted_no_id(self): + # Some documents may be encrypted but not have an /ID key in + # their trailer. Tests + # https://github.com/pdfminer/pdfminer.six/issues/594 + path = absolute_sample_path('encryption/encrypted_doc_no_id.pdf') + with open(path, 'rb') as fp: + parser = PDFParser(fp) + doc = PDFDocument(parser) + assert_equal(doc.info, + [{'Producer': b'European Patent Office'}])