Take the 2-minute tour ×

I'm using Python and Qt 4.4 and I have to print some pages. Initially I thought I'd use HTML with CSS to produce those pages. But HTML has some limitations.

Now the question is: is there anything that's better than HTML but just (or almost) as easy to use? Additionally, it should be GPL-compatible.


kdgregory & Mark G: The most obvious limitation is that I can't specify the printer margins. There is another problem: How do I add page numbers?

Jeremy French: One thing I have to print is a list of all the products someone ordered which can spread over a few pages.

share|improve this question
It would help if you listed the limitations that are standing in your way. Aside from the possibility that a CSS expert could eliminate them, the knowledge would prevent people from providing equally limited solutions. –  kdgregory Jan 19 '09 at 17:14
I wish you could +1 comments, +1 @kdgregory! I've used a Templating Engine and HTML/CSS successfully numerous times for printing documents. What limitations are you hitting that caused this question? –  Mark G Jan 19 '09 at 17:19
It may also help if you were to say the structure of what you want to print. If you have 100s of pages of documents there are different answers to if you want to print one page with very nice formatting. It may also help if you give us some idea of the source format. –  Jeremy French Jan 19 '09 at 17:25
See my answer, i think XSL-FO is definitely what you are looking for. It supports margins and page numbers, and also everything else you would want. –  baretta Jan 19 '09 at 18:07
Before you think it's HTML and CSS that's the limiting factor, check out what you can do with just that in a project like Prince (princexml.com). –  Roger Pate Jan 20 '09 at 13:47
show 1 more comment

9 Answers

up vote 21 down vote accepted

I have been fighting with printed (or PDF) output from Python for 8 years now and so far I came across the following approaches (in order of personal preference):

  • Using JasperReports via pyJasper (written by me) or JasperServer. You can use the WYSIWYG design tool iReport to define your layout. Your Python code will contact the Java based Jasper engine via HTTP and make it render a PDF (pyJasper handles that). We use that for a few thousand pages a day.
  • Use plain text output. You can't get any faster. We use that for a few hundred pages per day.
  • Use XSLT-FO. You also have to call a Java based rendering engine like FOB. Might result in performance issues but can be mitigated by having a long running Java server process - same approach than with Jasper. We use that for a few hundred pages per day but writing XSLT-FO documents made my head hurt. Not used for new code.
  • Generate LaTeX source and use a latex software package to render to PDF. Getting LaTeX to look like you like is quite difficult. But as long as you go with the provided LaTeX styles, you are fine. Not used in production at my shop.
  • PDF generation with the ReportLab Toolkit. Somewhat low level. Even more low level: FPDF. We use FPDF-Ruby for a few hundred pages a day. Took a lot of fiddeling to get the layout we wanted.
  • Directly generate Postscript. Strange but you nearly can't get more in terms of speed and control. We used that to generate contact sheets with a few hundred thousand Jpegs per day. Takes fiddling but is fun.
  • use troff/groff to generate Postscript/PDF. Very low level bute nice to do simple, high volume things. Never used it thus in production.

For orders, invoices and the like I highly recommend JasperReports. The ability to use a visual editor to define the layout is a huge time saver.

share|improve this answer
add comment

There's LaTeX. Not sure if that falls into the "as easy to use as html" category, but it's not hard.

share|improve this answer
And I doubt there is anything better for automatic print layout. –  Fabian Steeg Jan 19 '09 at 17:13
I think LaTeX is not compatible with the GPL licence. –  Georg Schölly Jan 19 '09 at 23:42
I love LaTeX but found it very hard to generate something with LaTex which doesn't look like the things Lamport & Co envisioned. Also LaTeX support of table like things is sub-par. –  mdorseif Jan 23 '09 at 6:05
@ndorseif: I wouldn't call anything from LaTeX sub-par, even tables that might be hard, but not necessarily bad. –  voyager Feb 8 '10 at 14:36
add comment

By print do you mean a printer? If so, check ReportLab's PDF tools.

from reportlab.pdfgen import canvas
from reportlab.lib.units import cm
c = canvas.Canvas("hello.pdf")
c.drawString(9*cm, 22*cm, "Hello World!")
share|improve this answer
@nosklo - Sounds more like a step back, might as well just use System.Drawing and draw on an Image and print that? Unless it takes care of paging, i.e. that "Hello World!" at that position needs to wrap. –  Mark G Jan 19 '09 at 17:17
ReportLabs also has a layout engine (Platypus) –  oefe Jan 19 '09 at 20:44
I like ReportLab (with its layout engine) and have used iText (PDF generation for Java, similar to ReportLab) and XSL-FO. In my opinion, after spending many hours trying to debug XSL-FO stylesheets, XSL-FO stands for 'XML, eFfed' objects'. ReportLab is much easier, faster, and -much- lighter weight. –  joeforker Jan 19 '09 at 22:23
add comment

XSL Formatting Objects (part of the The Extensible Stylesheet Language Family (XSL)) if you need total control over printed documents.

Then, you'll need a Formatting Objects processor, like FOP or Antenna house, to transform the XSL-FO document into PDF, or PostScript.

share|improve this answer
Not total control or anything reasonably like it. Perhaps enough control. –  Stephan Eggermont Jan 19 '09 at 18:56
Then you have not used it professionally. Just like HTML, it takes skill too. –  baretta Jan 19 '09 at 19:40
add comment

You might consider Sphinx, a package that translates reStructuredText files into various output formats, including HTML, and LaTeX, for printable PDF. It's licensed under BSD and is now the official Python documentation tool.

share|improve this answer
add comment

Have you seen http://www.w3.org/TR/css3-page/ ? The print media is highly customizable. In my new project I'm going to replace PDF generator by this one.

share|improve this answer
In theory this works, but I don't think there's a HTML+CSS3 renderer out there that supports print media. –  Georg Schölly Oct 7 '12 at 19:44
add comment

What's wrong with just using Qt's native printing?

share|improve this answer
I'd like to have the layout template separately and not include it into the source code. –  Georg Schölly Jan 19 '09 at 17:29
Are you sure you want end users to crash your program? –  Stephan Eggermont Jan 19 '09 at 19:03
No, but I find it easier to change various parts of an application this way. –  Georg Schölly Jan 19 '09 at 23:24
add comment

Or if you're on a mac, you could check out quartz bindings for Python, but it's obviously not GPL.

share|improve this answer
add comment

I came to like asciidoc. Basically you produce plain text. This can be enhanced a lot by using a templating system like Django templates, or Jinja2. This is in principal a lot like the XSLT-FO thingy described by mdorseif (you use a toolchain to produce docbook and then anything you like), but not so much of a headache. There is allready a nice toolchain wrapper that makes most things easy.

share|improve this answer
add comment

Your Answer


