tex.py 3.69 KB
Newer Older
1
#-*- coding: utf-8 -*-
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# Re2o est un logiciel d'administration développé initiallement au rezometz. Il
# se veut agnostique au réseau considéré, de manière à être installable en
# quelques clics.
#
# Copyright © 2017  Gabriel Détraz
# Copyright © 2017  Goulven Kermarec
# Copyright © 2017  Augustin Lemesle
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 24 25 26
"""tex.py
Module in charge of rendering some LaTex templates.
Used to generated PDF invoice.
"""
27

28

29 30 31 32 33
import tempfile
from subprocess import Popen, PIPE
import os
from datetime import datetime

chirac's avatar
chirac committed
34
from django.template.loader import get_template
35 36
from django.template import Context
from django.http import HttpResponse
chirac's avatar
chirac committed
37
from django.conf import settings
38
from django.utils.text import slugify
chirac's avatar
chirac committed
39 40 41 42 43 44 45


TEMP_PREFIX = getattr(settings, 'TEX_TEMP_PREFIX', 'render_tex-')
CACHE_PREFIX = getattr(settings, 'TEX_CACHE_PREFIX', 'render-tex')
CACHE_TIMEOUT = getattr(settings, 'TEX_CACHE_TIMEOUT', 86400)  # 1 day


46
def render_invoice(_request, ctx={}):
47 48 49 50
    """
    Render an invoice using some available information such as the current
    date, the user, the articles, the prices, ...
    """
51
    filename = '_'.join([
52
        'invoice',
53 54 55 56 57
        slugify(ctx.get('asso_name', "")),
        slugify(ctx.get('recipient_name', "")),
        str(ctx.get('DATE', datetime.now()).year),
        str(ctx.get('DATE', datetime.now()).month),
        str(ctx.get('DATE', datetime.now()).day),
58
    ])
59
    r = render_tex(_request, 'cotisations/factures.tex', ctx)
60 61 62
    r['Content-Disposition'] = 'attachment; filename="{name}.pdf"'.format(
        name=filename
    )
63 64
    return r

65

66
def create_pdf(template, ctx={}):
67 68 69 70 71 72 73 74 75 76
    """Creates and returns a PDF from a LaTeX template using pdflatex.

    It create a temporary file for the PDF then read it to return its content.

    Args:
        template: Path to the LaTeX template.
        ctx: Dict with the context for rendering the template.

    Returns:
        The content of the temporary PDF file generated.
77
    """
Dalahro's avatar
Dalahro committed
78
    context = Context(ctx)
79
    template = get_template(template)
Dalahro's avatar
Dalahro committed
80
    rendered_tpl = template.render(context).encode('utf-8')
81

Dalahro's avatar
Dalahro committed
82
    with tempfile.TemporaryDirectory() as tempdir:
83
        for _ in range(2):
Dalahro's avatar
Dalahro committed
84 85
            process = Popen(
                ['pdflatex', '-output-directory', tempdir],
86 87
                stdin=PIPE,
                stdout=PIPE,
Dalahro's avatar
Dalahro committed
88 89 90 91
            )
            process.communicate(rendered_tpl)
        with open(os.path.join(tempdir, 'texput.pdf'), 'rb') as f:
            pdf = f.read()
92 93 94 95 96

    return pdf


def render_tex(_request, template, ctx={}):
97 98 99
    """Creates a PDF from a LaTex templates using pdflatex.

    Calls `create_pdf` and send back an HTTP response for
100
    accessing this file.
101 102 103 104 105 106 107 108

    Args:
        _request: Unused, but allow using this function as a Django view.
        template: Path to the LaTeX template.
        ctx: Dict with the context for rendering the template.

    Returns:
        An HttpResponse with type `application/pdf` containing the PDF file.
109
    """
Hugo LEVY-FALK's avatar
Hugo LEVY-FALK committed
110
    pdf = create_pdf(template, ctx)
Dalahro's avatar
Dalahro committed
111 112 113
    r = HttpResponse(content_type='application/pdf')
    r.write(pdf)
    return r