views:

1052

answers:

7

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.

Edit:

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.

+14  A: 

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

U62
And I doubt there is anything better for automatic print layout.
Fabian Steeg
I think LaTeX is not compatible with the GPL licence.
Georg
mdorseif
@ndorseif: I wouldn't call anything from LaTeX sub-par, even tables that might be *hard*, but not necessarily *bad*.
voyager
+7  A: 

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!")
c.showPage()
c.save()
nosklo
@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
ReportLabs also has a layout engine (Platypus)
oefe
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
+1  A: 

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

Pesto
I'd like to have the layout template separately and not include it into the source code.
Georg
Are you sure you want end users to crash your program?
Stephan Eggermont
No, but I find it easier to change various parts of an application this way.
Georg
+3  A: 

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.

baretta
Not total control or anything reasonably like it. Perhaps enough control.
Stephan Eggermont
Then you have not used it professionally. Just like HTML, it takes skill too.
baretta
+3  A: 

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.

Jeff Bauer
+1  A: 

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

Koen Bok
+12  A: 

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.

mdorseif