Allow piping output to stdout

This commit is contained in:
James R. Barlow
2016-10-27 16:14:42 -07:00
parent f7387b0859
commit 2e4431cc63
2 changed files with 39 additions and 13 deletions
+21 -13
View File
@@ -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)
+18
View File
@@ -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)