diff --git a/ocrmypdf/main.py b/ocrmypdf/main.py index 5a52cec2..b376bf51 100755 --- a/ocrmypdf/main.py +++ b/ocrmypdf/main.py @@ -729,11 +729,13 @@ def generate_postscript_stub( pdf = pypdf.PdfFileReader(input_file) def from_document_info(key): - # pdf.documentInfo.get() DOES NOT work as expected + # pdf.documentInfo.get() DOES NOT behave as expected for a dict-like + # object, so call with precautions. TypeError may occur if the PDF + # is missing the optional document info section. try: s = pdf.documentInfo[key] return str(s) - except KeyError: + except (KeyError, TypeError): return '' pdfmark = { diff --git a/tests/resources/NOTE.rst b/tests/resources/NOTE.rst index 9c069dba..a3c2d5a2 100644 --- a/tests/resources/NOTE.rst +++ b/tests/resources/NOTE.rst @@ -4,29 +4,31 @@ copyright reasons. Test files do not necessarily produce perfect (or even good) OCR results. -+-------------------+--------------------------------------------------------------------------------+ -| File | Source | -+===================+================================================================================+ -| graph.pdf | Wikimedia | -+-------------------+--------------------------------------------------------------------------------+ -| c02-22.pdf | Project Gutenberg: https://www.gutenberg.org/files/76/76-h/images/c02-22.jpg | -+-------------------+--------------------------------------------------------------------------------+ -| LinnSequencer.jpg | Wikimedia_ | -+-------------------+--------------------------------------------------------------------------------+ -| congress.jpg | http://www.baxleystamps.com/litho/meiji/courts_1871.jpg | -+-------------------+--------------------------------------------------------------------------------+ -| blank.pdf | Blank page from Adobe Illustrator CC 2015 | -+-------------------+--------------------------------------------------------------------------------+ -| enormous.pdf | PNG file saved to PDF using img2pdf | -+-------------------+--------------------------------------------------------------------------------+ -| invalid.pdf | PDF file header followed by EOF marker; not valid | -+-------------------+--------------------------------------------------------------------------------+ -| multipage.pdf | several other files concatenated | -+-------------------+--------------------------------------------------------------------------------+ -| skew.pdf | skewed version of c02-22.PDF | -+-------------------+--------------------------------------------------------------------------------+ -| Test_Issue_28.pdf | file with some syntax errors | -+-------------------+--------------------------------------------------------------------------------+ ++---------------------+--------------------------------------------------------------------------------+ +| File | Source | ++=====================+================================================================================+ +| graph.pdf | Wikimedia | ++---------------------+--------------------------------------------------------------------------------+ +| c02-22.pdf | Project Gutenberg: https://www.gutenberg.org/files/76/76-h/images/c02-22.jpg | ++---------------------+--------------------------------------------------------------------------------+ +| LinnSequencer.jpg | Wikimedia_ | ++---------------------+--------------------------------------------------------------------------------+ +| congress.jpg | http://www.baxleystamps.com/litho/meiji/courts_1871.jpg | ++---------------------+--------------------------------------------------------------------------------+ +| blank.pdf | Blank page from Adobe Illustrator CC 2015 | ++---------------------+--------------------------------------------------------------------------------+ +| enormous.pdf | PNG file saved to PDF using img2pdf | ++---------------------+--------------------------------------------------------------------------------+ +| invalid.pdf | PDF file header followed by EOF marker; not valid | ++---------------------+--------------------------------------------------------------------------------+ +| multipage.pdf | several other files concatenated | ++---------------------+--------------------------------------------------------------------------------+ +| skew.pdf | skewed version of c02-22.PDF | ++---------------------+--------------------------------------------------------------------------------+ +| Test_Issue_28.pdf | file with some syntax errors | ++---------------------+--------------------------------------------------------------------------------+ +| missing_docinfo.pdf | file missing its DocumentInfo dictionary | ++---------------------+--------------------------------------------------------------------------------+ .. _Wikimedia: https://upload.wikimedia.org/wikipedia/en/b/b7/LinnSequencer_hardware_MIDI_sequencer_brochure_page_2_300dpi.jpg \ No newline at end of file diff --git a/tests/resources/missing_docinfo.pdf b/tests/resources/missing_docinfo.pdf new file mode 100644 index 00000000..0525e9e9 Binary files /dev/null and b/tests/resources/missing_docinfo.pdf differ diff --git a/tests/test_main.py b/tests/test_main.py index d94e0ef6..f170e1f5 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -308,3 +308,10 @@ def test_klingon(): p, out, err = run_ocrmypdf_env( 'francais.pdf', 'francais.pdf', '-l', 'klz') assert p.returncode == ExitCode.bad_args + + +def test_missing_docinfo(): + p, out, err = run_ocrmypdf_env( + 'missing_docinfo.pdf', 'missing_docinfo.pdf', '-l', 'eng') + assert p.returncode == ExitCode.ok, err +