By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,854 Members | 1,885 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,854 IT Pros & Developers. It's quick & easy.

printing HTML or PDF from Python

P: n/a
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
Jul 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Donnal Walter wrote:
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
Jul 18 '05 #2

P: n/a
Donnal Walter wrote:
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
Jul 18 '05 #3

P: n/a
Donnal Walter <do****@donnal.net> writes:
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
Jul 18 '05 #4

P: n/a
On Tue, 26 Oct 2004 12:14:45 -0400, Peter Hansen <pe***@engcorp.com> wrote:
Donnal Walter wrote:
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.


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

--
// Jorgen Grahn <jgrahn@ Ph'nglui mglw'nafh Cthulhu
\X/ algonet.se> R'lyeh wgah'nagl fhtagn!
Jul 18 '05 #5

P: n/a
Jorgen Grahn wrote:
On Tue, 26 Oct 2004 12:14:45 -0400, Peter Hansen <pe***@engcorp.com> wrote:
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 :-/


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
Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.