tex.py 3.67 KB
Newer Older
lhark's avatar
lhark committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
# 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.
22 23 24 25
"""tex.py
Module in charge of rendering some LaTex templates.
Used to generated PDF invoice.
"""
lhark's avatar
lhark committed
26

27 28 29 30 31
import tempfile
from subprocess import Popen, PIPE
import os
from datetime import datetime

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


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


44
def render_invoice(_request, ctx={}):
45 46 47 48
    """
    Render an invoice using some available information such as the current
    date, the user, the articles, the prices, ...
    """
49
    filename = '_'.join([
50
        'invoice',
51 52 53 54 55
        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),
56
    ])
57
    r = render_tex(_request, 'cotisations/factures.tex', ctx)
58 59 60
    r['Content-Disposition'] = 'attachment; filename="{name}.pdf"'.format(
        name=filename
    )
61 62
    return r

63

64
def create_pdf(template, ctx={}):
65 66 67 68 69 70 71 72 73 74
    """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.
75
    """
Dalahro's avatar
Dalahro committed
76
    context = Context(ctx)
77
    template = get_template(template)
Dalahro's avatar
Dalahro committed
78
    rendered_tpl = template.render(context).encode('utf-8')
79

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

    return pdf


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

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

    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.
107 108
    """
    pdf = create_pdf(template, ctx={})
Dalahro's avatar
Dalahro committed
109 110 111
    r = HttpResponse(content_type='application/pdf')
    r.write(pdf)
    return r