From 8b0496d35ed94f37d52303de39f182951342c96e Mon Sep 17 00:00:00 2001 From: "James R. Barlow" Date: Tue, 3 Jul 2018 22:38:10 -0700 Subject: [PATCH] Fix invalid XML characters choking parser --- src/ocrmypdf/pdfinfo.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/ocrmypdf/pdfinfo.py b/src/ocrmypdf/pdfinfo.py index af2b629d..d46f2175 100644 --- a/src/ocrmypdf/pdfinfo.py +++ b/src/ocrmypdf/pdfinfo.py @@ -36,6 +36,7 @@ Encoding = Enum('Encoding', 'ccitt jpeg jpeg2000 jbig2 asciihex ascii85 lzw flate ' + \ 'runlength') +regex_remove_char_tags = re.compile(br"") FRIENDLY_COLORSPACE = { '/DeviceGray': Colorspace.gray, @@ -503,6 +504,14 @@ def _page_get_textblocks(infile, pageno): import xml.etree.ElementTree as ET gstext = ghostscript.extract_text(infile, pageno+1) + + # Remove all tags, because they might contain invalid XML entities + # like which chokes on the + # inclusion of U+0001. Understandably. + # Just remove the whole tag since we don't use it at all, and they + # are only generated as innermost self-closing tags. + gstext = regex_remove_char_tags.sub(b' ', gstext) + root = ET.fromstring(gstext) def blocks():