Python 中类似于 Jinja 的 Pdf
我正在寻找 Python 中最准确的 PDF 工具,其工作方式类似于 Jinja 对 HTML 的作用。
您有什么建议?
I am looking for the best accurate tool for PDF in Python that works like Jinja does for HTML.
What are your suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
正如 jbochi 所回答的,ReportLab 是几乎所有生成 PDF 的 Python 项目的基础。
但为了满足您的需求,您可能需要查看 Pisa / xhtml2pdf。您可以使用 Jinja 模板生成 HTML,然后使用 Pisa 将 HTML 转换为 PDF。 Pisa 建立在 ReportLab 之上。
编辑:我忘记的另一个选项是wkhtmltopdf
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
查看 ReportLab 工具包。
不过,您只能在商业版本中使用模板。
Have a look at ReportLab Toolkit.
You can use templates only with the commercial version, though.
现在,这个街区出现了一个新成员,名为 WeasyPrint。
There's now a new kid on the block called WeasyPrint.
我和OP的要求完全相同。不幸的是,WeasyPrint 不是一个可行的解决方案,因为我需要非常精确的定位和条形码支持。经过几天的工作,我完成了一个支持 Jinja2 的 reportlab XML 包装器。
代码可以在 GitHub 上找到
包括一个示例 XML 生成以下 PDF。
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.
使用 rst2pdf 或 < 将 python/jinja 转换为 rst/html 并将 html/rst 转换为 pdf 怎么样? a href="http://johnmacfarlane.net/pandoc/" rel="nofollow">pandoc。
这两种方法对我来说都效果很好,但是。像plaes一样,我将来可能会尝试Weasyprint。
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.
还有什么比 Jinja 本身更准确的 Python 中的 PDF 工具,与 Jinja 一样工作呢?
您只需确保
Jinja
块、变量和注释标识字符串不与LaTeX
命令冲突。一旦您将Jinja
环境更改为模仿LaTeX
环境,您就可以开始了!下面是一个开箱即用的代码片段:
Python 源代码:
./create_pdf.py
Latex 模板:
./latex/latex_template .tex
现在只需调用:
$>; python ./create_pdf.py
生成的乳胶源:
./ generated_latex.tex
生成的 Pdf:
参考文献:
dict
到render_template
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 theLaTeX
commands. Once you change theJinja
environment to mimic theLaTeX
environment you're ready to go!Here's a snippet that works out of the box:
Python Source:
./create_pdf.py
Latex Template:
./latex/latex_template.tex
Now simply call:
$> python ./create_pdf.py
Resulting Latex Source:
./generated_latex.tex
Generated Pdf:
References:
dict
torender_template
如果您想使用现有的 PDF 作为模板,而不更改原始文档,您可以使用 Dhek 模板编辑器,它允许在单独的模板文件中定义区域(边界、名称、类型)。
模板以 JSON 格式保存,以便可以在 Python 中进行解析,以填充 PDF 上的区域并生成最终文档(例如,使用 Web 表单中的值)。
请参阅 https://github.com/applicius/dhek 处的文档。
[编辑]
最初的答案来自 dhek 的作者。
我已经使用过这个工具,如果您的表单不是以通常的方式生成的,那么这非常有用(它甚至适用于从图像完成的 PDF)。
下载、解压缩并运行 DHEK(无需安装,可移植)后,您可以选择区域并为其命名:
然后,您可以将“映射”保存为 JSON,以便获得区域的位置和尺寸:
然后您可以将这些位置与
reportlab
一起使用来创建包含文本的 PDF:然后您可以使用任何工具/pdf 库(例如
pdfrw
)将两者合并在一个页面中:(创建覆盖和合并的代码的最后一部分来自优秀博客“Mouse vs Python”:https://www.blog.pythonlibrary.org/2018/05/22/filling-pdf-forms-with-python /)
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 .
[EDIT]
Initial answer was from the author of dhek.
I have used this tool and this is great if your form has not been generated in the usual way (it even works on PDF done from images).
After you downloaded, unzipped, and run DHEK (no install needed, it is portable), you can select areas and given them a name:
You can then save the "mapping" to JSON so you can get the positions and dimensions of the areas:
You can then use these positions with
reportlab
to create a PDF that contains the text:You can then use any tool / pdf library (e.g.
pdfrw
) to merge the two in a single page:(last part of code to create Overlay and Merge is coming from the Excellent blog "Mouse vs Python": https://www.blog.pythonlibrary.org/2018/05/22/filling-pdf-forms-with-python/)
...还有用于此目的的库
pdfjinja
:https:// github.com/rammie/pdfjinja它使用注释来创建模板值。
在我的用例中,我没有带有正确表单字段的 PDF,因此 cchantep 建议的解决方案更合适。
... There is also the library
pdfjinja
that is for this purpose: https://github.com/rammie/pdfjinjaIt is using annotations to create template values.
In my use case, I didn't have a PDF with proper form fields so the solution suggested by cchantep was more suitable.