Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django/Python: generate pdf with the proper language

I use Pisa/xhtml2pdf in my Django apps to generate pdf from an HTML source. That is:

  1. I generate the HTML file formatted with all 'printing' stuffs (e.g. page-breaks, header, footer, etc.)
  2. I convert this HTML into pdf using Pisa

This process is ok but it is slow (expecially when dealing with long tables) and I must use HTML/CSS according to Pisa features/limitations.

The question is: is this the right way to generate pdf from a web application (i.e. create HTML and then convert it to pdf) or there is a more direct way, that is "write" the pdf with a more suitable language?

like image 540
Don Avatar asked Dec 19 '12 11:12

Don


1 Answers

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.

like image 189
Simon Sapin Avatar answered Oct 12 '22 21:10

Simon Sapin