views:

549

answers:

3

I'm trying to print a PDF document with the win32print module. Apparently this module can only accept PCL or raw text. Is that correct?

If so, is there a module available to convert a PDF document into PCL?

I contemplated using ShellExecute; however, this is not an option since it only allows printing to the default printer. I need to print to a variety of printers on servers across various networks.

Thanks for your help, Pete

A: 

I am not sure how to specifically get win32print to work, but there might be a couple of other options. Reportlab if often mentioned when creating PDFs from Python. If you are already invested in your approach, maybe using PyX or pypsg to generate the Postscript files and then feeding that into win32print would work.

John Paulett
I am already using ReportLab to create the PDF. Now it needs to be printed.
slypete
+2  A: 

I ended up using Ghostscript to accomplish this task. There is a command line tool that relies on Ghostscript called gsprint.

You don't even need Acrobat installed to print PDFs in this fashion wish is quite nice.

Here is an example:

on the command line:

gsprint -printer \\server\printer "test.pdf"

from python:

win32api.ShellExecute(0, 'open', 'gsprint.exe', '-printer "\\\\' + self.server + '\\' + self.printer_name + '" ' + file, '.', 0)

Note that I've added to my PATH variable in these examples, so I don't have to include the entire path when calling the executable.

There is one downside, however. The code is licensed under the GPL, so it's no very useful in commercial software.

Hope this helps someone, Pete

slypete
You can do it that way or have Ghostscript output to whatever lanugage you want that the printer supports such as PCL / Postscript. The benefit there is that you can then open the output file and insert commands for tray selection etc. if need.
Douglas Anderson
If you go with an older version of Ghostscript like 7.06/7.07 I believe these are licensed under GNU, as such it might resolve your licensing concern.
Douglas Anderson
GPL isn't necessarily a disadvantage. ;)
Georg
A: 

I was already using the win32api.ShellExecute approach and needed to print to a non-default printer. The best way I could work out was to temporarily change the default printer. So right before I do the print I store what the current default printer is, change it, and then set it back after printing. Something like:

tempprinter = "\\\\server01\\printer01"
currentprinter = win32print.GetDefaultPrinter()

win32print.SetDefaultPrinter(tempprinter)
win32api.ShellExecute(0, "print", filename, None,  ".",  0)
win32print.SetDefaultPrinter(currentprinter)

I'm not going to claim it's pretty, but it worked and it allowed me to leave my other code untouched.

Craig Smith