2015-07-27 15:22:00 -07:00
|
|
|
#!/usr/bin/env python3
|
2015-07-28 04:36:58 -07:00
|
|
|
# © 2015 James R. Barlow: github.com/jbarlow83
|
|
|
|
|
|
2015-07-27 15:22:00 -07:00
|
|
|
from tempfile import NamedTemporaryFile
|
|
|
|
|
from subprocess import Popen, PIPE, check_call
|
|
|
|
|
from shutil import copy
|
2015-12-17 09:05:10 -08:00
|
|
|
from . import get_program
|
2015-07-27 15:22:00 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def rasterize_pdf(input_file, output_file, xres, yres, raster_device, log):
|
|
|
|
|
with NamedTemporaryFile(delete=True) as tmp:
|
|
|
|
|
args_gs = [
|
2015-12-17 09:05:10 -08:00
|
|
|
get_program('gs'),
|
2015-08-28 04:51:36 -07:00
|
|
|
'-dQUIET',
|
|
|
|
|
'-dBATCH',
|
|
|
|
|
'-dNOPAUSE',
|
2015-07-27 15:22:00 -07:00
|
|
|
'-sDEVICE=%s' % raster_device,
|
|
|
|
|
'-o', tmp.name,
|
|
|
|
|
'-r{0}x{1}'.format(str(xres), str(yres)),
|
|
|
|
|
input_file
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
p = Popen(args_gs, close_fds=True, stdout=PIPE, stderr=PIPE,
|
|
|
|
|
universal_newlines=True)
|
|
|
|
|
stdout, stderr = p.communicate()
|
|
|
|
|
if stdout:
|
|
|
|
|
log.debug(stdout)
|
|
|
|
|
if stderr:
|
|
|
|
|
log.error(stderr)
|
|
|
|
|
|
|
|
|
|
if p.returncode == 0:
|
|
|
|
|
copy(tmp.name, output_file)
|
|
|
|
|
else:
|
|
|
|
|
log.error('Ghostscript rendering failed')
|
|
|
|
|
|
|
|
|
|
|
2015-07-30 23:20:04 -07:00
|
|
|
def generate_pdfa(pdf_pages, output_file, threads=1):
|
2015-07-27 15:22:00 -07:00
|
|
|
with NamedTemporaryFile(delete=True) as gs_pdf:
|
|
|
|
|
args_gs = [
|
2015-12-17 09:05:10 -08:00
|
|
|
get_program("gs"),
|
2015-07-27 15:22:00 -07:00
|
|
|
"-dQUIET",
|
|
|
|
|
"-dBATCH",
|
|
|
|
|
"-dNOPAUSE",
|
2015-07-30 23:20:04 -07:00
|
|
|
'-dNumRenderingThreads=' + str(threads),
|
2015-07-27 15:22:00 -07:00
|
|
|
"-sDEVICE=pdfwrite",
|
2016-02-07 03:20:42 -08:00
|
|
|
"-dAutoRotatePages=/None",
|
2015-07-27 15:22:00 -07:00
|
|
|
"-sColorConversionStrategy=/RGB",
|
|
|
|
|
"-sProcessColorModel=DeviceRGB",
|
2016-02-04 18:49:09 -08:00
|
|
|
"-dJPEGQ=95",
|
2015-12-02 01:48:10 -08:00
|
|
|
"-dPDFA=2",
|
2015-07-27 15:22:00 -07:00
|
|
|
"-sPDFACompatibilityPolicy=2",
|
|
|
|
|
"-sOutputICCProfile=srgb.icc",
|
|
|
|
|
"-sOutputFile=" + gs_pdf.name,
|
|
|
|
|
]
|
|
|
|
|
args_gs.extend(pdf_pages)
|
|
|
|
|
check_call(args_gs)
|
|
|
|
|
copy(gs_pdf.name, output_file)
|