473,395 Members | 1,468 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

printing HTML or PDF from Python

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
5 3299
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Fuzzyman | last post by:
I looked through the manual, I looked on the web and could find very little on this subject.... The closest I could find was : http://www.faqts.com/knowledge_base/view.phtml/aid/4549. Saying...
7
by: Kamilche | last post by:
I'm pretty desperate to get color syntax printing. I just tried out the evaluation version of 'Komodo', which several people in here suggested, and its print features are terrible. The text is...
2
by: David Isaac | last post by:
I'd like to try personal financial management using Python. I just found PyCheckbook, but it does not support check printing. Is there a Python check printing application kicking around? Thanks,...
8
by: David Isaac | last post by:
"Alan Isaac" <aisaac0@verizon.net> wrote in message news:_A34e.2207$1r6.248@trnddc02... > I'd like to try personal financial management using Python. > I just found PyCheckbook, but it does not...
6
by: Kamilche | last post by:
I have a large project that is getting complex, and I would like to print the docstrings without importing the modules. The only Python utility I could find references is apparently defunct and...
3
by: Max | last post by:
How can I print (as in laser printer, not the python print statement) HTML from Python (actually it doesn't have to be HTML - it's tabular data with some rows/columns highlited). The project...
3
by: defcon8 | last post by:
How can I print html documents in Python on Windows?
5
by: David | last post by:
All, Is there a pretty printing utility for Python, something like Tidy for HTML? That will change: xp=self.uleft+percentx*(self.xwidth) To:
2
by: David | last post by:
Hi list. I've never used unicode in a Python script before, but I need to now. I'm not sure where to start. I'm hoping that a kind soul can help me out here. My current (almost non-existant)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.