{{ #-------------------------------------------------------------------------- # # Converte to transform a tex file generate by a view as a pdf file # #-------------------------------------------------------------------------- import base64 import cStringIO import os from subprocess import call from tempfile import TemporaryFile from uuid import uuid4 # # Get 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() wd = os.path.join(request.folder, 'tmp_build') if not os.path.exists(wd): os.chdir(request.folder) os.mkdir('tmp_build') os.chdir('tmp_build') else: os.chdir(wd) pass fn = str(uuid4()) fi = open('%s.tex' % fn, 'wb') fi.write(latex) fi.close() # # Launch a subprocess to convert the latex file into pdf # cmd = ['pdflatex', '-interaction', 'nonstopmode', '%s.tex' % fn] call(cmd, stdout=TemporaryFile()) # # copy the pdf into a base64 string # fn_pdf = '%s.pdf' % fn if os.path.exists(fn_pdf): fi = open(fn_pdf, 'rb') data = fi.read() fi.close() else: data = "\nThe generation of the PDF file failed." data += "\nPlease, check the LaTeX file." pass s64 = base64.b64encode(data) # # cleaning # for ext in ('aux', 'log', 'out', 'pdf', 'tex'): f = '%s.%s' % (fn, ext) if os.path.exists(f): os.remove(f) pass pass os.chdir(cwd) # # Prepare the response # response.headers['Content-Type']='application/pdf' response.write(s64, escape=False) }}