From 40ef4f0bbe7b22f4d86ccc33f6050151d899d977 Mon Sep 17 00:00:00 2001 From: "James R. Barlow" Date: Wed, 28 Mar 2018 00:54:58 -0700 Subject: [PATCH] Add new argument --skip-repair to skip the repair step --- docs/batch.rst | 2 ++ src/ocrmypdf/__main__.py | 8 ++++++++ src/ocrmypdf/pipeline.py | 23 +++++++++++++---------- tests/test_main.py | 10 +++++++++- 4 files changed, 32 insertions(+), 11 deletions(-) diff --git a/docs/batch.rst b/docs/batch.rst index 8f206782..649e10a5 100644 --- a/docs/batch.rst +++ b/docs/batch.rst @@ -18,6 +18,8 @@ The ``--tag`` argument tells parallel to print the filename as a prefix whenever parallel --tag -j 2 ocrmypdf '{}' 'output/{}' ::: *.pdf +OCRmyPDF automaticaly repairs PDFs before parsing and gathering information from them. If you are already repairing PDFs with ``qpdf`` prior to attempting OCR, or you can use ``--skip-repair`` to skip this step. It may improve performance for large files, since repairing PDFs is single-threaded. + Directory trees --------------- diff --git a/src/ocrmypdf/__main__.py b/src/ocrmypdf/__main__.py index cbe6e027..b6a9f02c 100755 --- a/src/ocrmypdf/__main__.py +++ b/src/ocrmypdf/__main__.py @@ -322,6 +322,14 @@ advanced.add_argument( advanced.add_argument( '--user-patterns', metavar='FILE', help="Specify the location of the Tesseract user patterns file.") +advanced.add_argument( + '--skip-repair', action='store_true', + help="Normally OCRmyPDF automatically repairs PDFs using qpdf before " + "processing. If you have already run qpdf or a similar program " + "that repairs PDF errors, you can tell OCRmyPDF to skip repair with " + "this option. This may be helpful in batch processing where all " + "files are repaired prior to OCR occurs, since repair is single " + "threaded and time consuming for large files.") debugging = parser.add_argument_group( "Debugging", diff --git a/src/ocrmypdf/pipeline.py b/src/ocrmypdf/pipeline.py index 80bf6e91..869f3ffc 100644 --- a/src/ocrmypdf/pipeline.py +++ b/src/ocrmypdf/pipeline.py @@ -206,16 +206,19 @@ def triage( triage_image_file(input_file, output_file, log, options) -def repair_pdf( +def repair_and_parse_pdf( input_file, output_file, log, context): options = context.get_options() - log.debug("Beginning qpdf repair...") - qpdf.repair(input_file, output_file, log) + if not options.skip_repair: + log.debug("Beginning qpdf repair...") + qpdf.repair(input_file, output_file, log) + log.debug("Repair OK; beginning parse...") + else: + re_symlink(input_file, output_file, log) - log.debug("Repair OK; beginning parse...") pdfinfo = PdfInfo(output_file) if pdfinfo.has_userunit and options.output_type == 'pdfa': @@ -1112,8 +1115,8 @@ def build_pipeline(options, work_folder, log, context): output=os.path.join(work_folder, 'origin.pdf'), extras=[log, context]) - task_repair_pdf = main_pipeline.transform( - task_func=repair_pdf, + task_repair_and_parse_pdf = main_pipeline.transform( + task_func=repair_and_parse_pdf, input=task_triage, filter=suffix('.pdf'), output='.repaired.pdf', @@ -1123,7 +1126,7 @@ def build_pipeline(options, work_folder, log, context): # Split (kwargs for split seems to be broken, so pass plain args) task_pre_split_pages = main_pipeline.split( pre_split_pages, - task_repair_pdf, + task_repair_and_parse_pdf, os.path.join(work_folder, '*.presplit.pdf'), extras=[log, context]) @@ -1287,7 +1290,7 @@ def build_pipeline(options, work_folder, log, context): # PDF/A task_generate_postscript_stub = main_pipeline.transform( task_func=generate_postscript_stub, - input=task_repair_pdf, + input=task_repair_and_parse_pdf, filter=formatter(r'\.repaired\.pdf'), output=os.path.join(work_folder, 'pdfa.ps'), extras=[log, context]) @@ -1322,7 +1325,7 @@ def build_pipeline(options, work_folder, log, context): task_render_hocr_debug_page, task_skip_page, task_ocr_tesseract_and_render_pdf, - task_repair_pdf], + task_repair_and_parse_pdf], output=os.path.join(work_folder, 'merged.pdf'), extras=[log, context]) task_merge_pages_qpdf.active_if( @@ -1334,7 +1337,7 @@ def build_pipeline(options, work_folder, log, context): task_render_hocr_debug_page, task_skip_page, task_ocr_tesseract_and_render_pdf, - task_repair_pdf], + task_repair_and_parse_pdf], output=os.path.join(work_folder, 'merged.pdf'), extras=[log, context]) task_merge_pages_mupdf.active_if( diff --git a/tests/test_main.py b/tests/test_main.py index ed741042..34151d20 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -1068,4 +1068,12 @@ def test_output_is_symlink(spoof_tesseract_noop, resources, outdir): ) assert p.returncode == ExitCode.ok, err assert (outdir / 'out.pdf').stat().st_size > 0, 'target file not created' - \ No newline at end of file + + +def test_skip_repair(spoof_tesseract_noop, resources, outpdf): + check_ocrmypdf( + resources / 'trivial.pdf', + outpdf, + '--skip-repair', + env=spoof_tesseract_noop + ) \ No newline at end of file