类似 Jinja 的 Python 中的 Pdf

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2064936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 23:41:05  来源:igfitidea点击:

Jinja-like for Pdf in Python

pythonpdf-generationjinja2

提问by Mamane

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

我正在寻找 Python 中最准确的 PDF 工具,它的工作方式类似于 Jinja 对 HTML 所做的工作。

What are your suggestions?

你有什么建议?

采纳答案by Van Gale

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

正如 jbochi 所回答的,ReportLab 是几乎所有生成 PDF 的 Python 项目的基础。

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.

但是对于您的需求,您可能需要查看Pisa / xhtml2pdf。您将使用 Jinja 模板生成 HTML,然后使用 Pisa 将 HTML 转换为 PDF。Pisa 建立在 ReportLab 之上。

Edit:another option I'd forgotten about is wkhtmltopdf

编辑:我忘记的另一个选项是wkhtmltopdf

回答by jbochi

Have a look at ReportLab Toolkit.

看看ReportLab 工具包

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

但是,您只能将模板用于商业版本。

回答by plaes

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

现在街区里有一个新孩子叫WeasyPrint

回答by badzong

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.

我的要求与 OP 完全相同。不幸的是,WeasyPrint 不是一个可行的解决方案,因为我需要非常精确的定位和条形码支持。经过几天的工作,我完成了一个支持 Jinja2 的 reportlab XML 包装器。

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

代码可以在 GitHub 上找到,包括一个示例XML,它生成以下PDF

回答by Matteo

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

有什么比 Jinja 本身更像 Jinja 的 Python 中 PDF 更准确的工具?

You just have to make sure that the Jinjablock, variable, and comment identification strings do not conflict with the LaTeXcommands. Once you change the Jinjaenvironment to mimic the LaTeXenvironment you're ready to go!

您只需确保Jinja块、变量和注释标识字符串不与LaTeX命令冲突。一旦您改变Jinja环境以模仿LaTeX环境,您就可以开始了!

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

这是一个开箱即用的片段:

Python Source:./create_pdf.py

蟒蛇源:./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

乳胶模板:./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

现在只需调用: $> python ./create_pdf.py

Resulting Latex Source:./generated_latex.tex

结果乳胶来源:./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:

生成的PDF:

enter image description here

enter image description here

References:

参考:

回答by zach

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

python/jinja 到 rst/html 和 html/rst 到 pdf 使用rst2pdfpandoc 怎么样

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

这两个对我来说都很好,但是。像 plaes,我将来可能会尝试Weasyprint

回答by cchantep

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.

如果您想使用现有的 PDF 作为模板,而不改变原始文档,您可以使用 Dhek 模板编辑器,它允许在单独的模板文件中定义区域(边界、名称、类型)。

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).

模板以 JSON 格式保存,以便可以在 Python 中进行解析,以填充 PDF 上的区域并生成最终文档(例如,使用来自 Web 表单的值)。

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

请参阅https://github.com/applicius/dhek 上的文档。