Source code for uw.local.teaching.db.pdf
"""PDF processing.
This module implements additional PDF processing routines.
"""
from subprocess import Popen, PIPE
[docs]def embed_fonts (pdf):
"""Embed all fonts in the given PDF document.
:param pdf: The PDF document.
:return: A 2-tuple consisting of a boolean success indication and either
the PDF with fonts embedded or the error report from the sub-process.
"""
# TODO: This can hang after printing an error message on stderr.
# So really we need to simultaneously wait on output to stderr and
# on process termination. If we see output, wait a few seconds to
# capture any more output, then report an error.
pdftops_process = Popen (['pdftops', '-origpagesizes', '-', '-'], stdin=PIPE, stdout=PIPE, stderr=PIPE)
pdftops_process.stdin.write (pdf)
pdftops_process.stdin.close ()
# There doesn't seem to be any acceptable documentation of these
# options, but these are widely suggested as reasonable.
ps2pdf_process = Popen (['ps2pdf', '-dSAFER', '-dNOPLATFONTS', '-dEmbedAllFonts=true', '-dPDFSETTINGS=/prepress', '-', '-'], stdin=pdftops_process.stdout, stdout=PIPE, stderr=PIPE)
stdoutdata, stderrdata = ps2pdf_process.communicate ()
result = pdftops_process.wait ()
if result != 0:
return False, pdftops_process.stderr
result = ps2pdf_process.wait ()
if result != 0:
return False, stderrdata.split ('\n')
return True, stdoutdata