From 4ba9e8fe256f6c28c816da0fa4d183760751b44d Mon Sep 17 00:00:00 2001 From: "James R. Barlow" Date: Tue, 30 Oct 2018 22:28:34 -0700 Subject: [PATCH] Add AcroForm detection --- src/ocrmypdf/_pipeline.py | 19 +++++++++++++++++++ src/ocrmypdf/pdfinfo/__init__.py | 5 +++++ 2 files changed, 24 insertions(+) diff --git a/src/ocrmypdf/_pipeline.py b/src/ocrmypdf/_pipeline.py index 0d4dc87c..1e494b39 100644 --- a/src/ocrmypdf/_pipeline.py +++ b/src/ocrmypdf/_pipeline.py @@ -193,6 +193,25 @@ def repair_and_parse_pdf( "high page count files. Python 3.6 or newer is recommended." ) + if pdfinfo.has_acroform: + if options.redo_ocr: + log.error( + "This PDF has a user fillable form. --redo-ocr is not " + "currently possible on such files." + ) + raise PriorOcrFoundError() + else: + log.warning( + "This PDF has a fillable form. Chances are it is a pure digital " + "document that does not need OCR." + ) + if not options.force_ocr: + log.info( + "Use the option --force-ocr to produce an image of the " + "form and all filled form fields. The output PDF will be " + "'flattened' and will no longer be fillable." + ) + context.set_pdfinfo(pdfinfo) log.debug(pdfinfo) diff --git a/src/ocrmypdf/pdfinfo/__init__.py b/src/ocrmypdf/pdfinfo/__init__.py index 842340bd..b2f23879 100644 --- a/src/ocrmypdf/pdfinfo/__init__.py +++ b/src/ocrmypdf/pdfinfo/__init__.py @@ -803,6 +803,7 @@ class PdfInfo: self._infile = infile self._pages, pdf = _pdf_get_all_pageinfo(infile, log=log) self._needs_rendering = pdf.root.get('/NeedsRendering', False) + self._has_acroform = pdf.root.get('/AcroForm', False) @property def pages(self): @@ -817,6 +818,10 @@ class PdfInfo: def has_userunit(self): return any(page.userunit != 1.0 for page in self.pages) + @property + def has_acroform(self): + return self._has_acroform + @property def filename(self): if not isinstance(self._infile, (str, Path)):