Django/Python: generate pdf with the proper language Django/Python: generate pdf with the proper language python python

Django/Python: generate pdf with the proper language


WeasyPrint author here. The point of using HTML/CSS to generate PDF (vs. using a lower-level PDF library directly.) is to get automatic layout. It lets you specify high-level constraints like h1 { page-break-after: avoid } and let the layout engine figure it out, rather than specifying the absolute position of everything. The former is much more maintainable when you make changes to your documents.

Some tools like rst2pdf have their own stylesheet syntax, but that’s just a bad way of re-inventing CSS.

But yes, dumping complex stylesheets made for screen might not give great results. It’s better to build the stylesheets with print in mind, or even use completely different stylesheets with @media print in CSS or <link media="print"> in HTML.


I think generating a pdf from html with libraries like Pisa or http://weasyprint.org/ is the simplest approach. because it takes care of inserting images, css, barcode (on pisa) ... etc

If you want to write the pdf yourself take a look at Reportlab but it will take much longer to implement. In both cases i suggest to always generate the pdf in the background with celery or python-rq for optimization.


Pisa is known having various issues - especially with long tables. In general one should avoid using PISA. Other options are:

  • using Reportlab directly
  • z3c.rml (Reportlab template language clone)
  • commercial alternatives:
    • PrinceXML
    • PDFreactor

The general rule when it comes to PDF production: you get what you pay for.

Converters like Pisa or Apache FOP are half-baked solutions that work for simple cases but suck in general.