tex.py 4.11 KB
Newer Older
Gabriel Detraz's avatar
Gabriel Detraz committed
1
# -*- mode: python; coding: utf-8 -*-
lhark's avatar
lhark committed
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.
"""
lhark's avatar
lhark committed
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
39
import logging
chirac's avatar
chirac committed
40 41 42 43 44 45 46


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


47
def render_invoice(_request, ctx={}):
48 49 50 51
    """
    Render an invoice using some available information such as the current
    date, the user, the articles, the prices, ...
    """
Hugo LEVY-FALK's avatar
Hugo LEVY-FALK committed
52
    is_estimate = ctx.get('is_estimate', False)
53
    filename = '_'.join([
Hugo LEVY-FALK's avatar
Hugo LEVY-FALK committed
54
        'cost_estimate' if is_estimate else 'invoice',
55 56 57 58 59
        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),
60
    ])
61
    r = render_tex(_request, 'cotisations/factures.tex', ctx)
62 63 64
    r['Content-Disposition'] = 'attachment; filename="{name}.pdf"'.format(
        name=filename
    )
65 66
    return r

67

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

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

    return pdf


98 99 100 101 102 103 104 105 106 107 108 109 110 111
def escape_chars(string):
    """Escape the '%' and the '€' signs to avoid messing with LaTeX"""
    if not isinstance(string, str):
        return string
    mapping = (
        ('€', r'\euro'),
        ('%', r'\%'),
    )
    r = str(string)
    for k, v in mapping:
        r = r.replace(k, v)
    return r


112
def render_tex(_request, template, ctx={}):
113 114 115
    """Creates a PDF from a LaTex templates using pdflatex.

    Calls `create_pdf` and send back an HTTP response for
116
    accessing this file.
117 118 119 120 121 122 123 124

    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.
125
    """
Hugo LEVY-FALK's avatar
Hugo LEVY-FALK committed
126
    pdf = create_pdf(template, ctx)
Dalahro's avatar
Dalahro committed
127 128 129
    r = HttpResponse(content_type='application/pdf')
    r.write(pdf)
    return r