diff --git a/ocrmypdf/__main__.py b/ocrmypdf/__main__.py index 42208860..91f9db4e 100755 --- a/ocrmypdf/__main__.py +++ b/ocrmypdf/__main__.py @@ -1313,7 +1313,13 @@ def copy_final( pdfinfo, pdfinfo_lock): input_file = next((ii for ii in input_files if ii.endswith('.pdf'))) - shutil.copy(input_file, output_file) + + if output_file == '-': + from shutil import copyfileobj + with open(input_file, 'rb') as input_stream: + copyfileobj(input_stream, sys.stdout.buffer) + else: + shutil.copy(input_file, output_file) def available_cpu_count(): @@ -1470,20 +1476,22 @@ def run_pipeline(): _log.error(e) return ExitCode.other_error - if options.output_type == 'pdfa': - pdfa_info = file_claims_pdfa(options.output_file) - if pdfa_info['pass']: - msg = 'Output file is a {} (as expected)' - _log.info(msg.format(pdfa_info['conformance'])) - else: - msg = 'Output file was generated but is not PDF/A (seems to be {})' - _log.warning(msg.format(pdfa_info['conformance'])) + if options.output_file != '-': + if options.output_type == 'pdfa': + pdfa_info = file_claims_pdfa(options.output_file) + if pdfa_info['pass']: + msg = 'Output file is a {} (as expected)' + _log.info(msg.format(pdfa_info['conformance'])) + else: + msg = 'Output file is okay but is not PDF/A (seems to be {})' + _log.warning(msg.format(pdfa_info['conformance'])) + return ExitCode.invalid_output_pdf + if not qpdf.check(options.output_file, _log): + _log.warning('Output file: The generated PDF is INVALID') return ExitCode.invalid_output_pdf - - if not qpdf.check(options.output_file, _log): - _log.warning('Output file: The generated PDF is INVALID') - return ExitCode.invalid_output_pdf + else: + _log.info("Output sent to stdout") with _pdfinfo_lock: _log.debug(_pdfinfo) diff --git a/tests/test_main.py b/tests/test_main.py index 66ff711b..0f0771dc 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -662,6 +662,24 @@ def test_stdin(spoof_tesseract_noop): assert p.returncode == ExitCode.ok +def test_stdout(spoof_tesseract_noop): + input_file = _infile('francais.pdf') + output_file = _outfile('test_stdout.pdf') + + # Runs: ocrmypdf francais.pdf - > test_stdout.pdf + with open(output_file, 'wb') as output_stream: + p_args = OCRMYPDF + [input_file, '-'] + p = Popen( + p_args, close_fds=True, stdout=output_stream, stderr=PIPE, + stdin=DEVNULL, env=spoof_tesseract_noop) + out, err = p.communicate() + + assert p.returncode == ExitCode.ok + + from ocrmypdf import qpdf + assert qpdf.check(output_file, log=None) + + def test_masks(spoof_tesseract_noop): check_ocrmypdf('masks.pdf', 'test_masks.pdf', env=spoof_tesseract_noop)