From 4ab0dad146161afbe870a92d13cddc66364fbbf7 Mon Sep 17 00:00:00 2001 From: Hugo LEVY-FALK Date: Wed, 15 Aug 2018 23:56:34 +0200 Subject: [PATCH] Documentation propre de render_tex et create_pdf --- cotisations/tex.py | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/cotisations/tex.py b/cotisations/tex.py index 4487d5b8..f3f8601b 100644 --- a/cotisations/tex.py +++ b/cotisations/tex.py @@ -62,15 +62,23 @@ def render_invoice(_request, ctx={}): def create_pdf(template, ctx={}): - """ - Creates and returns a PDF from a LaTeX template using pdflatex. + """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. """ context = Context(ctx) template = get_template(template) rendered_tpl = template.render(context).encode('utf-8') with tempfile.TemporaryDirectory() as tempdir: - for i in range(2): + for _ in range(2): process = Popen( ['pdflatex', '-output-directory', tempdir], stdin=PIPE, @@ -84,10 +92,18 @@ def create_pdf(template, ctx={}): def render_tex(_request, template, ctx={}): - """ - Creates a PDF from a LaTex templates using pdflatex. - Writes it in a temporary directory and send back an HTTP response for + """Creates a PDF from a LaTex templates using pdflatex. + + Calls `create_pdf` and send back an HTTP response for accessing this file. + + 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. """ pdf = create_pdf(template, ctx={}) r = HttpResponse(content_type='application/pdf')