{{include 'lists/index.html'}} {{ import base64 import cStringIO import os from subprocess import call from tempfile import TemporaryFile from uuid import uuid4 # # go to the build directory # 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 # # create the HTML file # shtml = '%s' % response.body.getvalue() froot = str(uuid4()) fhtml = '%s.html' % froot fi = open(fhtml, 'wb') fi.write(shtml) fi.close() # # convert the HTML in ODT file using libreoffice # fi_tmp = TemporaryFile() # create the virtual X11 environment on the server if os.path.exists('/usr/bin/Xvfb'): cmd = "Xvbf :1 -screen 0 800x600x8 & " cmd += "libreoffice --nologo --headless --invisible --display :1.0 --convert-to odt " cmd += fhtml call(cmd, stdout=fi_tmp, shell=True) # development area in which X11 is running else: cmd = ["libreoffice", "--headless", "--convert-to", "odt", fhtml] call(cmd, stdout=fi_tmp) pass fodt = '%s.odt' % froot if os.path.exists(fodt): fi = open(fodt, 'rb') sodt = fi.read() fi.close() else: call("rm -f %s.*" % froot, shell=True) os.chdir(cwd) fi_tmp.seek(0) msg = T("The generation of the ODT file failed:

") msg += fi_tmp.read() fi_tmp.close() raise HTTP(500, msg) pass # # clean # for ext in ('html', 'odt'): f = '%s.%s' % (froot, ext) if os.path.exists(f): os.remove(f) pass pass os.chdir(cwd) # # 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) }}