Platform independent search for iccprofiles for PDF/A

This commit is contained in:
Jim Barlow
2015-07-24 01:18:46 -07:00
parent 289e4025ad
commit 8c0dc9a06d
2 changed files with 34 additions and 4 deletions
+2 -2
View File
@@ -458,8 +458,8 @@ def render_page(
log,
pdfinfo,
pdfinfo_lock):
hocr = [ii for ii in infiles if ii.endswith('.hocr')][0]
image = [ii for ii in infiles if ii.endswith('.png')][0]
hocr = next(ii for ii in infiles if ii.endswith('.hocr'))
image = next(ii for ii in infiles if ii.endswith('.page.png'))
pageinfo = get_pageinfo(image, pdfinfo, pdfinfo_lock)
dpi = round(max(pageinfo['xres'], pageinfo['yres']))
+32 -2
View File
@@ -7,8 +7,13 @@
from __future__ import print_function, absolute_import, division
from string import Template
from subprocess import Popen, PIPE
import os
# This is a template written in PostScript which is needed to create PDF/A
# files, from the Ghostscript documentation. Lines beginning with % are
# comments. Python substitution variables have a '$' prefix.
pdfa_def_template = u"""%!
% This is a sample prefix file for creating a PDF/A document.
% Feel free to modify entries marked with "Customize".
@@ -60,10 +65,35 @@ def _get_pdfa_def(icc_profile, pdf_title, icc_identifier):
return result
def _get_postscript_icc_path():
"Parse Ghostscript's help message to find where iccprofiles are stored"
p_gs = Popen(['gs', '--help'], close_fds=True, universal_newlines=True,
stdout=PIPE, stderr=PIPE)
out, _ = p_gs.communicate()
lines = out.splitlines()
def search_paths(lines):
seeking = True
for line in lines:
if seeking:
if line.startswith('Search path'):
seeking = False
continue
else:
if line.strip().startswith('/'):
yield from (
path.strip() for path in line.split(':')
if path.strip() != '')
for root in search_paths(lines):
path = os.path.realpath(os.path.join(root, '../iccprofiles'))
if os.path.exists(path):
return path
def generate_pdfa_def(target_filename, pdf_title='', icc='sRGB'):
# How does find this directory on other platforms?
if icc == 'sRGB':
icc_profile = '/usr/local/share/ghostscript/iccprofiles/srgb.icc'
icc_profile = os.path.join(_get_postscript_icc_path(), 'srgb.icc')
else:
raise NotImplementedError("Only supporting sRGB")