Print a PDF transparently

Tim Golden tim.golden at viacom-outdoor.co.uk
Mon Feb 20 09:19:37 EST 2006


[Daniel Crespo]
| 
| > Have you seen this?
| >     http://tgolden.sc.sabren.com/python/win32_how_do_i/print.html
| > In particular, the section on using win32print directly.
| 
| Yes, I have. The problems is that an external program is launched for
| handling the file and print it. 

[sorry, bit long-winded, but trying to help]

I think the poster was referring to the situation you 
described where the problem reduced to sending a .ps
file directly to a postscript-capable printer.

To put things in as much of a nutshell as I can, I
think the following are true:

1) You have a document in some format which
you wish to print either via Postscript or PDF.

2) You do have postscript-enabled printers available.

3) As far as possible, you don't really want to install 
external apps across 70 offices.

What's not entirely clear is the extent to which you
can automate the generation of a postscript doc. Let's
assume that this can be done centrally. If that's the
case then you can take the file and use the technique
described in the link above, but specifically in this
section:

http://tgolden.sc.sabren.com/python/win32_how_do_i/print.html#win32print

which hasn't to do with running Acrobat or Ghostscript.

Basically, adapting the example, you'd do this:

<code>
import win32print
printer_name = win32print.GetDefaultPrinter ()
#
# raw_data could equally be raw PCL/PS read from 
#  some print-to-file operation
#
name_of_postscript_file = "document.ps"
raw_data = open (name_of_postscript_file, "rb").read ()

hPrinter = win32print.OpenPrinter (printer_name)
try:
  hJob = win32print.StartDocPrinter (hPrinter, 1, ("test of raw data",
None, "RAW"))
  try:
    win32print.WritePrinter (hPrinter, raw_data)
  finally:
    win32print.EndDocPrinter (hPrinter)
finally:
  win32print.ClosePrinter (hPrinter)

</code>

If you're not really sure how to get a postscript
doc, I usually just install an AppleLaserwriter of some
description as a driver, printing to file, and just
generate things that way. Exactly how you do that
depends on what the original document is.

Now, if the original is in PDF (ie PDF isn't simply
the staging post I've assumed it was), you'll need
to use *something* like ghostscript to print or
to generate postscript. Automating Acrobat Reader 
is a nightmare, because there's always something 
you don't want to happen -- it stays open, or you 
can't specify the printer to use, or something.
There are other PDF-display tools out there
(someone mentioned one recently on c.l.py and
there's xpdf as well, which might help).

TJG

________________________________________________________________________
This e-mail has been scanned for all viruses by Star. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________



More information about the Python-list mailing list