{{include 'lists/index.html'}} {{ import base64 import cStringIO import os from subprocess import call, check_output from uuid import uuid4 # # go to the /tmp directory # cwd = os.getcwd() os.chdir('/tmp') # # file names # froot = str(uuid4()) fhtml = '%s.html' % froot fodt = '%s.odt' % froot # # create the HTML file in /tmp # shtml = """ %s""" % response.body.getvalue() fi = open(fhtml, 'wb') fi.write(shtml) fi.close() # # convert the HTML in ODT file using LibreOffice # LibreOffice headless requires an HOME directory for .config/libreoffice # try: cmd = "export HOME=/tmp && libreoffice --headless --convert-to odt %s" sout = check_output(cmd % fhtml, shell=True) if os.path.exists(fodt): fi = open(fodt, 'rb') sodt = fi.read() fi.close() else: msg = T("The generation of the ODT file failed:

") msg += sout raise HTTP(500, msg) pass finally: call(["rm", "-f", "/tmp/%s.*" % froot]) os.chdir(cwd) pass # # build the response # response.body = cStringIO.StringIO() response.headers['Content-Type']='application/vnd.oasis.opendocument.text' response.headers['Content-Disposition'] = 'attachment;filename="list.odt"' s64 = base64.b64encode(sodt) response.write(s64, escape=False) }}