Jinja-like for Pdf in Python

人走茶凉 提交于 2020-03-17 05:44:48

问题


I am looking for the best accurate tool for PDF in Python that works like Jinja does for HTML.

What are your suggestions?


回答1:


As answered by jbochi, ReportLab is the foundation for almost all Python projects that generate PDF.

But for your needs you might want to check out Pisa / xhtml2pdf. You would generate your HTML with a Jinja template and then use Pisa to convert the HTML to PDF. Pisa is built on top of ReportLab.

Edit: another option I'd forgotten about is wkhtmltopdf




回答2:


Have a look at ReportLab Toolkit.

You can use templates only with the commercial version, though.




回答3:


There's now a new kid on the block called WeasyPrint.




回答4:


I had exactly the same requirement as the OP. Unfortunately WeasyPrint wasn't a viable solution, because I needed very exact positioning and barcode support. After a few days of work I finished a reportlab XML wrapper with Jinja2 support.

The code can be found on GitHub including an example XML wich generates the following PDF.




回答5:


What about python/jinja to rst/html and html/rst to pdf using either rst2pdf or pandoc.

Both of these have worked well for me but. like plaes, I may try Weasyprint in the future.




回答6:


What more accurate tool for PDF in Python that works like Jinja than Jinja itself?

You just have to make sure that the Jinja block, variable, and comment identification strings do not conflict with the LaTeX commands. Once you change the Jinja environment to mimic the LaTeX environment you're ready to go!

Here's a snippet that works out of the box:

Python Source: ./create_pdf.py

import os, jinja2
from jinja2 import Template

latex_jinja_env = jinja2.Environment(
    block_start_string    = '\BLOCK{',
    block_end_string      = '}',
    variable_start_string = '\VAR{',
    variable_end_string   = '}',
    comment_start_string  = '\#{',
    comment_end_string    = '}',
    line_statement_prefix = '%%',
    line_comment_prefix   = '%#',
    trim_blocks           = True,
    autoescape            = False,
    loader                = jinja2.FileSystemLoader(os.path.abspath('./latex/'))
)
template = latex_jinja_env.get_template('latex_template.tex')

# populate a dictionary with the variables of interest
template_vars  = {}
template_vars['section_1'] = 'The Section 1 Title'
template_vars['section_2'] = 'The Section 2 Title'

# create a file and save the latex
output_file = open('./generated_latex.tex', 'w')
# pass the dictionary with variable names to the renderer
output_file.write( template.render( template_vars ) )
output_file.close()

Latex Template: ./latex/latex_template.tex

\documentclass{article}
\begin{document}
\section{Example}
An example document using \LaTeX, Python, and Jinja.

% This is a regular LaTeX comment
\section{\VAR{section_1}}
\begin{itemize}
\BLOCK{ for x in range(0,3) }
  \item Counting: \VAR{x}
\BLOCK{ endfor }
\end{itemize}

\#{This is a long-form Jinja comment}
\BLOCK{ if subsection_1_1 }
\subsection{ The subsection }
This appears only if subsection_1_1 variable is passed to renderer.
\BLOCK{ endif }

%# This is a short-form Jinja comment
\section{\VAR{section_2}}
\begin{itemize}
%% for x in range(0,3)
  \item Counting: \VAR{x}
%% endfor
\end{itemize}

\end{document}

Now simply call: $> python ./create_pdf.py

Resulting Latex Source: ./generated_latex.tex

\documentclass{article}
\begin{document}
\section{Example}
An example document using \LaTeX, Python, and Jinja.

% This is a regular LaTeX comment
\section{The Section 1 Title}
\begin{itemize}
  \item Counting: 0
  \item Counting: 1
  \item Counting: 2
\end{itemize}

\section{The Section 2 Title}
\begin{itemize}
  \item Counting: 0
  \item Counting: 1
  \item Counting: 2
\end{itemize}

\end{document}

Generated Pdf:

References:

  • About Django's syntax conflict with LaTex
  • About latex templates
  • About passing a dict to render_template



回答7:


If you want to use existing PDF as template, without altering original document, you can use Dhek template editor, which allows to define area (bounds, name, type) in a separate template file.

Template is saved in JSON format so that it can be parsed in Python, to fill areas over PDF and generate the final document (e.g. with values from Web form).

See documentation at https://github.com/applicius/dhek .



来源:https://stackoverflow.com/questions/2064936/jinja-like-for-pdf-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!