Skip to content
Snippets Groups Projects
Commit 7a897195 authored by tux091's avatar tux091
Browse files

Fully running view translating a latex file into pdf.

parent 013f62a3
No related branches found
No related tags found
No related merge requests found
{{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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment