Skip to content

Instantly share code, notes, and snippets.

@sugiana
Created March 30, 2015 15:15
Show Gist options
  • Save sugiana/dea1d4c7dc755772c99f to your computer and use it in GitHub Desktop.
Save sugiana/dea1d4c7dc755772c99f to your computer and use it in GitHub Desktop.
import os
import subprocess
from appy.pod.renderer import Renderer
from time import time
from pyramid.view import view_config
from pyramid.response import (
Response,
FileIter,
)
from ..tools import get_settings
@view_config(route_name='odt-pdf')
def view_odt_pdf(request):
settings = get_settings()
d = dict(nm='Owo Sugiana')
template_odt = os.path.join(settings['static_files'], 'test.odt')
result_dir = '/tmp'
result_pdf = 'result.pdf'
result_odt_ = os.path.join(result_dir, 'result.odt')
result_pdf_ = os.path.join(result_dir, result_pdf)
for file in [result_odt_, result_pdf_]:
if os.path.exists(file):
os.unlink(file)
renderer = Renderer(template_odt, d, result_odt_)
renderer.run()
command = 'libreoffice --headless --invisible --convert-to pdf {result_odt} --outdir {result_dir}'.format(
result_odt=result_odt_, result_dir=result_dir)
subprocess.Popen(command, shell=True)
awal = time()
while time() - awal < 10:
if os.path.exists(result_pdf_):
break
if not os.path.exists(result_pdf_):
return # Timeout
headers = [('Content-Disposition', 'attachment; filename={n}'.format(n=result_pdf))]
response = Response(content_type='application/pdf',
headerlist=headers)
f = open(result_pdf_, 'rb')
response.app_iter = FileIter(f)
return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment