pdf.py 687 Bytes
Newer Older
1 2 3 4 5 6
# -*- coding: utf-8 -*-

from flask import abort, make_response, render_template
from xhtml2pdf import pisa
from io import BytesIO

Nicolas Joyard's avatar
Nicolas Joyard committed
7
from ..models import Parlementaire
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31


def setup_routes(app):

    @app.route('/demande/<id>', endpoint='demande_pdf')
    def demande_pdf(id):
        parl = Parlementaire.query.filter_by(id=id).first()

        if not parl:
            abort(404)

        html = render_template('demande.html.j2',
            parlementaire=parl
        )



        pdf = BytesIO()
        pisa.CreatePDF(html, pdf)
        response = make_response(pdf.getvalue())
        pdf.close()

        response.headers['Content-Type'] = 'application/pdf'
        return response