pdf.py

About this file

The files contains the class LatexCompiler, which is used to create PDF from a LaTeX layout. The _create_file of the class creates the .tex file in the output directory and writes the data inside the inputted document into the .tex file.
This file also contains functions that generates a flask response with a pdf file attached, a LaTeX template for an envelope, and a PDF envelope. In addition, the escape_latex_characters function replaces characters with the LaTeX version.

The function 'compile(self, document)' compiles a LaTeX document to PDF.

Function 'generate_pdf(pdf_data)' generates a PDF from a string of data by returning a PDF File object.

Function 'generate_pdf_flask_response(pdf_data)' returns a Flask response object with a PDF as an attachment.

Function 'generate_envelope(template_name, data)' returns the LaTeX for an envelope with the provided data pre-filled.

Function 'generate_envelope_pdf(document)' returns a PDF envelope.

Function 'escape_latex_characters(line)' replaces a string with the escaped LaTeX version of reserved characters.

Code Issues

  • Line 91 uses partially qualified path with Jinja2. Jinja2 is a Python HTML templating system. It is typically used to build web applications, though appears in other places well, notably the Ansible automation system. When configuring the Jinja2 environment, the option to use autoescaping on input can be specified. When autoescaping is enabled, Jinja2 will filter input strings to escape any HTML content submitted via template variables. Without escaping HTML input the application becomes vulnerable to Cross Site Scripting (XSS) attacks. Unfortunately, autoescaping is False by default. Thus this plugin test will warn on omission of an autoescape setting, as well as an explicit setting of false.

Code Check Report


app/lib/pdf.py:38:121: E501 line too long (121 > 120 characters)
app/lib/pdf.py:116:31: W605 invalid escape sequence '\&'
app/lib/pdf.py:117:31: W605 invalid escape sequence '\%'
app/lib/pdf.py:118:31: W605 invalid escape sequence '\$'
app/lib/pdf.py:119:31: W605 invalid escape sequence '\#'
app/lib/pdf.py:120:31: W605 invalid escape sequence '\_'
app/lib/pdf.py:121:31: W605 invalid escape sequence '\{'
app/lib/pdf.py:122:31: W605 invalid escape sequence '\}'
                        

Documentation drawn from source code


LatexCompiler:

Generates a PDF from a LaTeX file.

Slightly modified from: https://github.com/AKuederle/flask-template-master/

__init__(self, latex_command=None):

_create_file(self, document):

compile(self, document):

Compile a LaTeX document to PDF.
:param document: LaTeX formatted document (UTF-8 Formatted String)
:return: PDF File Object

generate_pdf(pdf_data):

Generate a PDF from a string of data.
:param pdf_data: String of data to input into PDF.
:return: PDF File object

generate_pdf_flask_response(pdf_data):

Return a Flask response object with a PDF as an attachment.
:param pdf_data: String of data to input into PDF.
:return: Flask Response

generate_envelope(template_name, data):

Generate the LaTeX for an envelope with the provided data pre-filled.

:param template_name: The LaTeX template to be used.
:param data: Data to be filled in to the LaTeX template (Dict())
:return: LaTeX document

generate_envelope_pdf(document):

Generate a PDF envelope.
:param document: LaTeX document.
:return: PDF File Object

escape_latex_characters(line):

Replace a string with the escaped LaTeX version of reserved characters
:param line: a string to be used in a LaTeX template
:return: a string with the escaped LaTeX version of reserved characters

Source code