printing HTML or PDF from Python

D

Donnal Walter

We want to be able to print HTML or PDF files to a given printer from
Python in a kind of batch mode (without opening a preview window or
printer dialog for each file). The printer is on a network, but for
testing purposes I have connected it to my desktop via LPT1. I can
easily print a text file from Python using:

os.system("copy some.txt LPT1")

And as expected, using this function with "some.html" simply prints the
html source. (But I tried it anyway. :) I presume that one would need
to go through a Web browser of some kind to print the formatted document
(using CSS, BTW), but we don't want the user to see the browser window,
or be required to click on the Print button, etc. Is there a way to do
this from a Python script? We are currently deployed on Windows, but
would like to keep the application cross-platform if possible. We have
Mozilla installed, for example, if that would be of use, but I've
searched the Mozilla mailing list archives without seeing anything about
printing via Mozilla without actually opening a browser window.

The same more or less goes for PDF files. We have Acrobat Reader
installed, but I can't see how to print a PDF file from Python via AR
without opening the file inside the reader and manually printing each
one. Any suggestions would be much appreciated.

Regards,
Donnal Walter
Arkansas Children's Hospital
 
D

Daniel Dittmar

Donnal said:
We want to be able to print HTML or PDF files to a given printer from
Python in a kind of batch mode (without opening a preview window or
printer dialog for each file). The printer is on a network, but for

os.system ('"C:\Program Files\Adobe\Acrobat 6.0\Reader\AcroRd32.exe"' +
' /p /h ' + yourpdf)

This still brings up an empry Adobe Reader window.

General Strategy:
- does right-click on such a file shows a Print entry?
- if yes, try to find the definition for that entry in the registry
- HKEY_CLASSES_ROOT/.html => htmlfile
- look for HKEY_CLASSES_ROOT/htmlfile/shell/Print/command

Doing this using the _winreg module is left as an exercise.

I guess the command line options for Acrobat are the same on Linux (-p -h).

Mozilla doesn't seem to have a command line option to print
(http://www.mozilla.org/docs/command-line-args.html).

Daniel
 
P

Peter Hansen

Donnal said:
os.system("copy some.txt LPT1")
And as expected, using this function with "some.html" simply prints the
html source.

That's of course because the printer knows how to handle "raw" text
without further help. As you surmised, anything more complicated
needs either native support (e.g. Postscript can be sent to a
Postscript printer naturally), or an application to be launched
to interpret the file and "draw" it for the printer.
The same more or less goes for PDF files. We have Acrobat Reader
installed, but I can't see how to print a PDF file from Python via AR
without opening the file inside the reader and manually printing each
one. Any suggestions would be much appreciated.

I don't think you'll find a cross-platform approach to handle
all your needs, but there are a few options. The only one
that I'm familiar with at all would be using ActiveX to
control IE (for HTML files) and Acrobat Reader (for PDF)
through the documented interfaces. From Python it's not
generally very hard to do this, and I suspect the archives
have examples. (I haven't done the printing part myself, but
have done lots of IE-through-ActiveX and it's pretty
straightforward.)

-Peter
 
T

Thomas Heller

Donnal Walter said:
We want to be able to print HTML or PDF files to a given printer from
Python in a kind of batch mode (without opening a preview window or
printer dialog for each file). The printer is on a network, but for
testing purposes I have connected it to my desktop via LPT1. I can
easily print a text file from Python using:

os.system("copy some.txt LPT1")

And as expected, using this function with "some.html" simply prints
the html source. (But I tried it anyway. :) I presume that one would
need to go through a Web browser of some kind to print the formatted
document (using CSS, BTW), but we don't want the user to see the
browser window, or be required to click on the Print button, etc. Is
there a way to do this from a Python script? We are currently deployed
on Windows, but would like to keep the application cross-platform if
possible. We have Mozilla installed, for example, if that would be of
use, but I've searched the Mozilla mailing list archives without
seeing anything about printing via Mozilla without actually opening a
browser window.

The same more or less goes for PDF files. We have Acrobat Reader
installed, but I can't see how to print a PDF file from Python via AR
without opening the file inside the reader and manually printing each
one. Any suggestions would be much appreciated.

On windows, you would use the ShellExecute function, with 'print' as the
second parameter. Available in pywin32. Python itself also exposes it
as the os.startfile function, but unfortunately this doesn't accept a
second parameter.

Thomas
 
J

Jorgen Grahn

That's of course because the printer knows how to handle "raw" text
without further help. As you surmised, anything more complicated
needs either native support (e.g. Postscript can be sent to a
Postscript printer naturally), or an application to be launched
to interpret the file and "draw" it for the printer.

Or a print spooler daemon, which is the normal case on Unix. The user is
supposed to know what formats are supported by a specific spool, and only
send those into the spool. Postscript is the traditional lingua franca, but
I guess many Linux and other systems support PDF today. All this have
nothing to do with what the printer supports.

....
I don't think you'll find a cross-platform approach to handle
all your needs, but there are a few options. The only one
that I'm familiar with at all would be using ActiveX to
control IE (for HTML files) and Acrobat Reader (for PDF)
through the documented interfaces. From Python it's not

But that's not very cross-platform, is it?
I know of no cross-platform way :-/

/Jorgen
 
P

Peter Hansen

Jorgen said:
But that's not very cross-platform, is it?
I know of no cross-platform way :-/

Definitely not cross-platform, as I attempted to note.
(My "there are a few options" should perhaps have said
"there are options if you are willing to give that up"
or something.)

Donnal did say "cross platform if possible", but is
clearly working on Windows so I figured they were valid
options.

-Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top