Jinja-like for Pdf in Python

15,505

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.

Share:
15,505

Related videos on Youtube

Mamane
Author by

Mamane

Updated on April 17, 2022

Comments

  • Mamane
    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
    Mamane over 14 years
    You're right and it is a problem since I would like to work from a template in pdf format.
  • jbochi
    jbochi over 14 years
    Maybe it's an overkill, but you could use Jinja to generate the text and the toolkit to convert the text to pdf.
  • user1066101
    user1066101 over 14 years
    @jbochi: Not overkill, IMO. I think this solution works very well.
  • Sos
    Sos over 4 years
    How do you actually write to pdf?
  • Matteo
    Matteo over 4 years
    the script above returns the source code, so use whichever latex compiler you prefer.
  • Sos
    Sos over 4 years
    Yes, I found it in the meanwhile using pdflatex :)