473,739 Members | 2,385 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3328
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\Acr obat 6.0\Reader\Acro Rd32.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_RO OT/.html => htmlfile
- look for HKEY_CLASSES_RO OT/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
2776
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 that printing (to a printer :-) wasn't easy (at least on windows - let alone *cross* platform) - and that was dated 2000. Oh and - http://mail.python.org/pipermail/python-list/2002-September/121462.html
7
3131
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 coming out huge when I print at 8 point, tiny when I'm printing at 14 point (so tiny that it looks like subscripts), the margins are reset at random, and so on. Plus it doesn't recognize all my fonts. I don't care if the pretty printer is in an...
2
2057
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, Alan Isaac
8
2425
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 support check printing. > Is there a Python check printing application kicking around? OK, I'll assume silence means "no", so new question: What is the current best practice for cross platform printing of PostScript files from Python?
6
3057
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 hasn't been updated in 4 years. I don't care how spartan the output is - it could look exactly like python's internal docstrings, for all I care. It would be a nice added bonus if it printed to HTML, but not if it greatly increased the interface...
3
1920
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 currently uses Python 2.4 with wxPython and pywin/winpy (whatever it's called, for MS Access ODBC), so if it can be done easily in one of those, that'd be great, but if there's a module which makes it a breeze, I'm happy to download that too. --Max
3
2086
by: defcon8 | last post by:
How can I print html documents in Python on Windows?
5
2080
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
1604
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) knowledge of unicode: string types. What I don't understand yet is what encodings are and when you'd want/need to use them. What I'd like is to just be able to
0
8794
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9483
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9341
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9269
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8216
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4572
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4826
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2748
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2195
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.