diff --git a/views/reports/report_2.pdf b/views/reports/report_2.pdf new file mode 100644 index 0000000000000000000000000000000000000000..3c8b1d473f8a10f5da33fb90b7dd1d75ad34ff28 --- /dev/null +++ b/views/reports/report_2.pdf @@ -0,0 +1,44 @@ +{{include 'reports/report_2.latex'}} + +{{ +import cStringIO +import os + +from subprocess import call +from tempfile import TemporaryFile +from uuid import uuid4 + +# recuperate the latex string generate by the latex view +# and remove it from the response object +latex = response.body.getvalue() +response.body = cStringIO.StringIO() + +# go to the private directory and create a latex file +cwd = os.getcwd() +os.chdir(os.path.join(request.folder, 'private')) + +fn = str(uuid4()) +fi = open('%s.tex' % fn, 'wb') +fi.write(latex) +fi.close() + +# convert the latex file into pdf +cmd = ['pdflatex', '-interaction', 'nonstopmode', '%s.tex' % fn] +call(cmd, stdout=TemporaryFile()) + +# copy the pdf in the new response +fi = open('%s.pdf' % fn, 'rb') +response.headers['Content-Type']='application/pdf' +response.write(fi.read(), escape=False) + +# clean file +for ext in ('aux', 'log', 'pdf', 'tex'): + f = '%s.%s' % (fn, ext) + if os.path.exists(f): + os.remove(f) + pass +pass + +# goback to the web2py main directory +os.chdir(cwd) +}} \ No newline at end of file