Source code for uw.web.pdf.util

"""PDF-related utility routines.

Intended to provide PDF-related utility routines for Web applications.
Currently just contains a handler for running a process that produces a PDF and
serving the result to the Web user agent.
"""

from subprocess import Popen, PIPE, STDOUT

from ll.xist.ns import html

from uw.web.wsgi.status import HTTPInternalServerError

[docs]def handle_pdf_result (process_args, title, message): """Run an external process which generates a PDF result. Parameters: process_args -- a list of arguments to the process; title -- the filename for the resulting PDF file; message -- message to use in case of an error. A process is started using Popen, and the output collected. If the process is successful, the PDF output of the process is returned along with the specified PDF title filename. Otherwise, an HTTPInternalServerError is thrown with the provided message and showing the error output from the process. This is meant to be invoked from a WSGI handler created using wsgi.function.return_pdf. """ process = Popen (process_args, stdout=PIPE, stderr=PIPE) stdout, stderr = process.communicate () result = process.wait () if result == 0: return [stdout, title] else: result = [] result.append (html.p (message)) result.append (html.p ('Error:')) result.append (html.pre (repr (line) + '\n' for line in stderr.decode ().split ('\n'))) result.append (html.p ('Output:')) result.append (html.pre (repr (line) + '\n' for line in stdout.decode ().split ('\n'))) raise HTTPInternalServerError (result)