{{ #-------------------------------------------------------------------------- # # Convert latex file into pdf # #-------------------------------------------------------------------------- import base64 import io import os from subprocess import call, check_output from uuid import uuid4 # # file names # froot = str(uuid4()) ftex = '%s.tex' % froot fpdf = '%s.pdf' % froot # # Create latex file in /tmp # cwd = os.getcwd() os.chdir('/tmp') with open(ftex, 'bw') as fi: fi.write(response.body.getvalue().encode()) pass # # convert the latex file into pdf # try: cmd = ['pdflatex', '-interaction', 'nonstopmode', ftex] call(cmd) if os.path.exists(fpdf): fi = open(fpdf, 'rb') spdf = fi.read() fi.close() else: msg = "The generation of the PDF file failed.
" msg += "Please, check the LaTeX file." raise HTTP(500, T(msg)) pass finally: call("rm -f %s.*" % froot, shell=True) os.chdir(cwd) pass # # Prepare the response # response.headers['Content-Type']='application/pdf' response.body = io.StringIO() s64 = base64.b64encode(spdf).decode("utf-8") response.write(s64, escape=False) }}