Jinja-like for Pdf in Python
Solution 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
Solution 2
Have a look at ReportLab Toolkit.
You can use templates only with the commercial version, though.
Solution 3
There's now a new kid on the block called WeasyPrint.
Solution 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.
Solution 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.
Related videos on Youtube
Mamane
Updated on April 17, 2022Comments
-
Mamane about 2 years
I am looking for the best accurate tool for PDF in Python that works like Jinja does for HTML.
What are your suggestions?
-
Mamane over 14 yearsYou're right and it is a problem since I would like to work from a template in pdf format.
-
jbochi over 14 yearsMaybe it's an overkill, but you could use Jinja to generate the text and the toolkit to convert the text to pdf.
-
user1066101 over 14 years@jbochi: Not overkill, IMO. I think this solution works very well.
-
Sos over 4 yearsHow do you actually write to pdf?
-
Matteo over 4 yearsthe script above returns the source code, so use whichever latex compiler you prefer.
-
Sos over 4 yearsYes, I found it in the meanwhile using
pdflatex
:)