2

I am developing an application which has to be able to print a couple of pages with Python. Now I am searching for a method to create these pages and print them. It should work on Linux and Windows. The pages contain tables, images and text.

I developed the GUI with PyGtk, but I think it's convenient to create an image or PDF and print it. I have no idea how to do this. Anyone knows a good way for this?

Note: The problem isn't the generation. It is the printing of that file.

Nick Stinemates
  • 41,511
  • 21
  • 59
  • 60
svenwltr
  • 17,002
  • 12
  • 56
  • 68
  • You mean the OS-specific printing interface? How can the OS-specific printing API be magically OS independent? – S.Lott Apr 06 '11 at 20:37
  • I think a wrapper for these OS-specific printing API could be an good idea. – svenwltr Apr 06 '11 at 20:41
  • Printing is quite complex. Considering the variety of printing devices, (and protocols), and OS API's, I can't see how this is possible. – S.Lott Apr 06 '11 at 20:43
  • @S.Lott: Hmm... thats why I'm asking here ;-) I'll try it with GTK – svenwltr Apr 06 '11 at 20:50
  • Asking here doesn't make it possible. When Google doesn't turn up much, that is your biggest hint that it's so difficult no one has succeeded. – S.Lott Apr 06 '11 at 20:53

3 Answers3

4

GTK+ has its own printing system, but if you're looking for something Python specific, check out Reportlab.

Printing on Linux is pretty easy with CUPS (just use lp). On Windows, there's no native PDF support though you could print the generated file using the Adobe Reader ActiveX control (here's an example of doing it from .NET). Apparently ShellExecute also works here to tell Adobe Reader to print though I'm not sure how automated it is (i.e. whether you get a dialog box you don't want).

Community
  • 1
  • 1
Nicholas Riley
  • 43,532
  • 6
  • 101
  • 124
  • Yes, I found the GTK printing system too, but I don't find any good example or a description how to use it. – svenwltr Apr 06 '11 at 20:37
  • 2
    If you've already got code to draw with Cairo on the screen then you can just draw to the printing context. If not, investigate Reportlab. There seem to be a bunch of apps that print from PyGtk you can use as sample code; Google Code Search can be of some help (http://google.com/codesearch?hl=en&lr=&q=gtk.PrintOperation%5C%28%5C%29). – Nicholas Riley Apr 06 '11 at 20:40
2

ReportLab is probably what you're looking for.

You may also want to try XHTML2PDF. It's very easy to use. Create your XHTML template. Use Jinja2 (or similar) to fill in the template. Convert to PDF.

S.Lott
  • 384,516
  • 81
  • 508
  • 779
1

Have you considered generating HTML?

DShook
  • 14,833
  • 9
  • 45
  • 55
  • 1
    Why not PDF? I think the chances of getting the intended layout is higher when using PDF than when using HTML where you don't know which browser will be used to display and print it.- – ThiefMaster Apr 06 '11 at 20:26
  • @ThiefMaster It all depends on how strict you want the output to be and how much time you want to spend generating it. Making HTML is a quick way that is fairly consistent across platforms. – DShook Apr 06 '11 at 20:28
  • 1
    The problem isn't the generation. It is the printing of that file. – svenwltr Apr 06 '11 at 20:28
  • 1
    @ThiefMaster: HTML is something that's easy to understand and trivial for almost anyone to generate either directly or with lots of widely-available easy-to-use libraries. Dealing with things like PDFs and PostScript is less trivial and less widely understood. – Nicholas Knight Apr 06 '11 at 20:29