Browse Source

Documentation propre de render_tex et create_pdf

override_mails
Hugo LEVY-FALK 7 years ago
parent
commit
4ab0dad146
  1. 28
      cotisations/tex.py

28
cotisations/tex.py

@ -62,15 +62,23 @@ def render_invoice(_request, ctx={}):
def create_pdf(template, 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) context = Context(ctx)
template = get_template(template) template = get_template(template)
rendered_tpl = template.render(context).encode('utf-8') rendered_tpl = template.render(context).encode('utf-8')
with tempfile.TemporaryDirectory() as tempdir: with tempfile.TemporaryDirectory() as tempdir:
for i in range(2): for _ in range(2):
process = Popen( process = Popen(
['pdflatex', '-output-directory', tempdir], ['pdflatex', '-output-directory', tempdir],
stdin=PIPE, stdin=PIPE,
@ -84,10 +92,18 @@ def create_pdf(template, ctx={}):
def render_tex(_request, template, ctx={}): def render_tex(_request, template, ctx={}):
""" """Creates a PDF from a LaTex templates using pdflatex.
Creates a PDF from a LaTex templates using pdflatex.
Writes it in a temporary directory and send back an HTTP response for Calls `create_pdf` and send back an HTTP response for
accessing this file. 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={}) pdf = create_pdf(template, ctx={})
r = HttpResponse(content_type='application/pdf') r = HttpResponse(content_type='application/pdf')

Loading…
Cancel
Save