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

can't find a way to display and print pdf through python.

P: n/a
hello all,
I am stuck with a strange requirement.
I need a library that can help me display a pdf file as a report and
also want a way to print the same pdf file in a platform independent
way.
if that's not possible then I at least need the code for useing some
library for connecting to acrobat reader and giving the print command
on windows and some thing similar on ubuntu linux.
the problem is that I want to display reports in my application. the
user should be able to view the formatted report on screen and at his
choice click the print button on the screen to send it to the printer.
I have reportlab installed and that is sufficient to generate pdf reports.
but I still can't fine the way to display it directly and print it directly.
Please help me.
Krishnakant.
Feb 11 '07
Share this Question
Share on Google+
17 Replies


P: n/a
Are you trying to:
a) Make the PDF file open in it's default application?
b) Create a PDF-reader in Python?

....because your question is somewhat unclear. Report Lab has no PDF viewer.
You would need a PDF/PostScript parser to do that and that's more of a job
than I think you're looking for.
Feb 11 '07

P: n/a
well yes,
I need to view reports on line and also print them from within my app.
so yes I need to display the pdf in my app and print it as well.
regards.
Krishnakant
Feb 11 '07

P: n/a
krishnakant Mane wrote:
hello all,
I am stuck with a strange requirement.
I need a library that can help me display a pdf file as a report and
also want a way to print the same pdf file in a platform independent
way.
if that's not possible then I at least need the code for useing some
library for connecting to acrobat reader and giving the print command
on windows and some thing similar on ubuntu linux.
the problem is that I want to display reports in my application. the
user should be able to view the formatted report on screen and at his
choice click the print button on the screen to send it to the printer.
I have reportlab installed and that is sufficient to generate pdf reports.
but I still can't fine the way to display it directly and print it
directly.
Please help me.
Krishnakant.
Just let the registered .PDF viewer do it for you.

os.start('myfile.pdf')

Launches whatever is registered as .PDF viewer and user
can then print, save, zoom, etc. on their own.

-Larry
Feb 12 '07

P: n/a
On 2007-02-12, Larry Bates <la*********@websafe.comwrote:
>I at least need the code for useing some library for
connecting to acrobat reader and giving the print command on
windows and some thing similar on ubuntu linux.
Just let the registered .PDF viewer do it for you.

os.start('myfile.pdf')
Eh? I don't see os.start() it either 2.5 or 2.44
documentation, and it's sure not there in 2.4.3:

$ python
Python 2.4.3 (#1, Dec 10 2006, 22:09:09)
[GCC 3.4.6 (Gentoo 3.4.6-r1, ssp-3.4.5-1.0, pie-8.7.9)] on linux2
Type "help", "copyright", "credits" or "license" for more
information.
>>import os
print os.start
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: 'module' object has no attribute 'start'

I did find os.startfile() in the docs, but it's shown as
windows-only (it's not present under Linux).
Launches whatever is registered as .PDF viewer and user
can then print, save, zoom, etc. on their own.
Really?

--
Grant Edwards grante Yow! Civilization is
at fun! Anyway, it keeps
visi.com me busy!!
Feb 12 '07

P: n/a
Grant Edwards wrote:
On 2007-02-12, Larry Bates <la*********@websafe.comwrote:
>>I at least need the code for useing some library for
connecting to acrobat reader and giving the print command on
windows and some thing similar on ubuntu linux.
>Just let the registered .PDF viewer do it for you.

os.start('myfile.pdf')

Eh? I don't see os.start() it either 2.5 or 2.44
documentation, and it's sure not there in 2.4.3:

$ python
Python 2.4.3 (#1, Dec 10 2006, 22:09:09)
[GCC 3.4.6 (Gentoo 3.4.6-r1, ssp-3.4.5-1.0, pie-8.7.9)] on linux2
Type "help", "copyright", "credits" or "license" for more
information.
>>import os
>>print os.start
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: 'module' object has no attribute 'start'

I did find os.startfile() in the docs, but it's shown as
windows-only (it's not present under Linux).
>Launches whatever is registered as .PDF viewer and user
can then print, save, zoom, etc. on their own.

Really?
My bad. os.system()

-Larry
Feb 12 '07

P: n/a
os.start does not work.. attribute error.
regards.
Krishnakant.
Feb 12 '07

P: n/a
On 2007-02-12, Larry Bates <la*********@websafe.comwrote:
On 2007-02-12, Larry Bates <la*********@websafe.comwrote:
>>
>>>I at least need the code for useing some library for
connecting to acrobat reader and giving the print command on
windows and some thing similar on ubuntu linux.
>>Just let the registered .PDF viewer do it for you.

os.start('myfile.pdf')

Eh? I don't see os.start() it either 2.5 or 2.44
documentation, and it's sure not there in 2.4.3:

My bad. os.system()
That doesn't work either:

$ ls -l user.pdf
-rw------- 1 grante users 35640 2005-11-21 14:33 user.pdf

$ python
Python 2.4.3 (#1, Dec 10 2006, 22:09:09)
[GCC 3.4.6 (Gentoo 3.4.6-r1, ssp-3.4.5-1.0, pie-8.7.9)] on linux2
Type "help", "copyright", "credits" or "license" for more
information.
>>import os
os.system('user.pdf')
sh: user.pdf: command not found
32512
>>>
--
Grant Edwards grante Yow! Eisenhower!! Your
at mimeograph machine upsets
visi.com my stomach!!
Feb 12 '07

P: n/a
On Feb 12, 3:06 pm, "krishnakant Mane" <researchb...@gmail.comwrote:
os.start does not work.. attribute error.
regards.
Krishnakant.
It's os.system('thefile') or os.system('start thefile') but
that's windows only. It seems like there's no
platform-independent way to launch a preferred application to open
a file, but...

based on <http://cweiske.de/howto/launch/allinone.html>
I would suggest (by looking at sys.platform) to use "start" on
windows, "open" on mac, and on linux to use the xdg-open shell
script which is part of XdgUtils...
<http://portland.freedesktop.org/wiki/>

It works standalone. Even seems it would not take too much
effort to port it to python if you want.
Feb 12 '07

P: n/a
Grant Edwards wrote:
On 2007-02-12, Larry Bates <la*********@websafe.comwrote:
>On 2007-02-12, Larry Bates <la*********@websafe.comwrote:
>>>>I at least need the code for useing some library for
connecting to acrobat reader and giving the print command on
windows and some thing similar on ubuntu linux.
Just let the registered .PDF viewer do it for you.

os.start('myfile.pdf')
Eh? I don't see os.start() it either 2.5 or 2.44
documentation, and it's sure not there in 2.4.3:
My bad. os.system()

That doesn't work either:

$ ls -l user.pdf
-rw------- 1 grante users 35640 2005-11-21 14:33 user.pdf

$ python
Python 2.4.3 (#1, Dec 10 2006, 22:09:09)
[GCC 3.4.6 (Gentoo 3.4.6-r1, ssp-3.4.5-1.0, pie-8.7.9)] on linux2
Type "help", "copyright", "credits" or "license" for more
information.
>>import os
>>os.system('user.pdf')
sh: user.pdf: command not found
32512
>>>
Works fine on my system. You linux guys just have it hard.
The op said "windows". I can't answer for ubuntu linux but
maybe you can help there?

-Larry
Feb 12 '07

P: n/a
On 2007-02-12, Larry Bates <la*********@websafe.comwrote:
Grant Edwards wrote:
>On 2007-02-12, Larry Bates <la*********@websafe.comwrote:
>>On 2007-02-12, Larry Bates <la*********@websafe.comwrote:
>I at least need the code for useing some library for
>connecting to acrobat reader and giving the print command on
>windows and some thing similar on ubuntu linux.
Just let the registered .PDF viewer do it for you.
>
os.start('myfile.pdf')
Eh? I don't see os.start() it either 2.5 or 2.44
documentation, and it's sure not there in 2.4.3:
My bad. os.system()

That doesn't work either:

$ ls -l user.pdf
-rw------- 1 grante users 35640 2005-11-21 14:33 user.pdf

$ python
Python 2.4.3 (#1, Dec 10 2006, 22:09:09)
[GCC 3.4.6 (Gentoo 3.4.6-r1, ssp-3.4.5-1.0, pie-8.7.9)] on linux2
Type "help", "copyright", "credits" or "license" for more
information.
> >>import os
>>os.system('user.pdf')
sh: user.pdf: command not found
32512
> >>>

Works fine on my system. You linux guys just have it hard.
The op said "windows".
The posting to which you replied specified Linux.
I can't answer for ubuntu linux but maybe you can help there?
I don't see how. Pdf files just aren't executable.

--
Grant Edwards grante Yow! FIRST, I'm covering
at you with OLIVE OIL and
visi.com PRUNE WHIP!!
Feb 13 '07

P: n/a
Grant Edwards kirjoitti:
On 2007-02-12, Larry Bates <la*********@websafe.comwrote:
>Grant Edwards wrote:
>>On 2007-02-12, Larry Bates <la*********@websafe.comwrote:
On 2007-02-12, Larry Bates <la*********@websafe.comwrote:
>>I at least need the code for useing some library for
>>connecting to acrobat reader and giving the print command on
>>windows and some thing similar on ubuntu linux.
>Just let the registered .PDF viewer do it for you.
>>
>os.start('myfile.pdf')
Eh? I don't see os.start() it either 2.5 or 2.44
documentation, and it's sure not there in 2.4.3:
My bad. os.system()
That doesn't work either:

$ ls -l user.pdf
-rw------- 1 grante users 35640 2005-11-21 14:33 user.pdf

$ python
Python 2.4.3 (#1, Dec 10 2006, 22:09:09)
[GCC 3.4.6 (Gentoo 3.4.6-r1, ssp-3.4.5-1.0, pie-8.7.9)] on linux2
Type "help", "copyright", "credits" or "license" for more
information.
>>import os
>>os.system('user.pdf')
sh: user.pdf: command not found
32512
>>>
Works fine on my system. You linux guys just have it hard.
The op said "windows".

The posting to which you replied specified Linux.
>I can't answer for ubuntu linux but maybe you can help there?

I don't see how. Pdf files just aren't executable.
On Windows, this (where fileName is xyz.PDF, for example):
webbrowser.open(r'file://' + fileName)
starts Acrobat Reader with the document read in. I have no idea why,
because Acrobat Reader sure ain't my browser;)

Maybe someone could try this out on Linux.

Cheers,
Jussi
Feb 13 '07

P: n/a
On Feb 13, 7:44 pm, Jussi Salmela <tiedon_j...@hotmail.comwrote:
Grant Edwards kirjoitti:
On 2007-02-12, Larry Bates <larry.ba...@websafe.comwrote:
Grant Edwards wrote:
On 2007-02-12, Larry Bates <larry.ba...@websafe.comwrote:
On 2007-02-12, Larry Bates <larry.ba...@websafe.comwrote:
>I at least need the code for useing some library for
>connecting to acrobat reader and giving the print command on
>windows and some thing similar on ubuntu linux.
Just let the registered .PDF viewer do it for you.
>>>>os.start('myfile.pdf')
Eh? I don't see os.start() it either 2.5 or 2.44
documentation, and it's sure not there in 2.4.3:
My bad. os.system()
That doesn't work either:
> $ ls -l user.pdf
-rw------- 1 grante users 35640 2005-11-21 14:33 user.pdf
> $ python
Python 2.4.3 (#1, Dec 10 2006, 22:09:09)
[GCC 3.4.6 (Gentoo 3.4.6-r1, ssp-3.4.5-1.0, pie-8.7.9)] on linux2
Type "help", "copyright", "credits" or "license" for more
information.
>>import os
>>os.system('user.pdf')
sh: user.pdf: command not found
32512
Works fine on my system. You linux guys just have it hard.
The op said "windows".
The posting to which you replied specified Linux.
I can't answer for ubuntu linux but maybe you can help there?
I don't see how. Pdf files just aren't executable.

On Windows, this (where fileName is xyz.PDF, for example):
webbrowser.open(r'file://' + fileName)
starts Acrobat Reader with the document read in. I have no idea why,
because Acrobat Reader sure ain't my browser;)

Maybe someone could try this out on Linux.

Cheers,
Jussi
Works on Ubuntu -- this opens a tab in my browser and then launches
Document Viewer to view the PDF.

peter@astro:~$ python
Python 2.4.4c1 (#2, Oct 11 2006, 21:51:02)
[GCC 4.1.2 20060928 (prerelease) (Ubuntu 4.1.1-13ubuntu5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>import webbrowser
webbrowser.open(r'file:///home/peter/appa.pdf')


Feb 13 '07

P: n/a
On Feb 12, 4:56 pm, Larry Bates <larry.ba...@websafe.comwrote:
krishnakant Mane wrote:
hello all,
I am stuck with a strange requirement.
I need a library that can help me display a pdf file as a report and
also want a way to print the same pdf file in a platform independent
way.
if that's not possible then I at least need the code for useing some
library for connecting to acrobat reader and giving the print command
on windows and some thing similar on ubuntu linux.
the problem is that I want to display reports in my application. the
user should be able to view the formatted report on screen and at his
choice click the print button on the screen to send it to the printer.
I have reportlab installed and that is sufficient to generate pdf reports.
but I still can't fine the way to display it directly and print it
directly.
Please help me.
Krishnakant.

Just let the registered .PDF viewer do it for you.

os.start('myfile.pdf')

Launches whatever is registered as .PDF viewer and user
can then print, save, zoom, etc. on their own.

-Larry

os.startfile('pathToTheFile') should work. Under Library Reference- >
os -Process Management

Feb 13 '07

P: n/a
On 2007-02-11, krishnakant Mane <re**********@gmail.comwrote:
hello all,
I am stuck with a strange requirement.
I need a library that can help me display a pdf file as a report and
also want a way to print the same pdf file in a platform independent
way.
if that's not possible then I at least need the code for useing some
library for connecting to acrobat reader and giving the print command
on windows and some thing similar on ubuntu linux.
the problem is that I want to display reports in my application. the
user should be able to view the formatted report on screen and at his
choice click the print button on the screen to send it to the printer.
I have reportlab installed and that is sufficient to generate pdf reports.
but I still can't fine the way to display it directly and print it directly.
Please help me.
Krishnakant.
I think the best way to handle this under a unix like system is by using
the mailcap module, something like the following:

import mailcap
import os

mc = mailcap.getcaps()
cmd = mailcap.findmatch(mc, 'image/pdf' , filename='/tmp/dummy')[0]
os.system(cmd)

--
Antoon Pardon
Feb 13 '07

P: n/a

I'm using Suse Linux which has five program that
will open a pdf file from a shell command line.
Acrobat Reader is one of the five.

A right button click on the file should give a
list of programs that will open a PDF file.

Im using a KDE desktop
Open a shell, add the path to the directory where
the PDF file lives, and on the command line type;

xpdf myfile.pdf ## may also work on a Gnome
## Desktop
or any of these for the KDE desktop;

kpdf myfile.pdf
Kghostview myfile.pdf
konqueror myfile.pdf ## my browser + myfile.

Acrobat Reader is also listed on my system. It
open files as adobe reader using the mouse to
open a pdf file but not from the command line
with an arg, and I haven't spent the time to
figgure out why.
You will have to import os and some combination of
any of the exec.. args, or the popen, spawn, or
system modules.

os.execvep() ## or others like execl, execle ....
os.spawnv(),
os.spawnve(),
os.popen()

hope this give some direction.

jim-on-linux



On Tuesday 13 February 2007 03:44, Jussi Salmela
wrote:
Grant Edwards kirjoitti:
On 2007-02-12, Larry Bates
<la*********@websafe.comwrote:
Grant Edwards wrote:
On 2007-02-12, Larry Bates
<la*********@websafe.comwrote:
>>On 2007-02-12, Larry Bates
<la*********@websafe.comwrote:
>>>>>I at least need the code for useing
>some library for connecting to acrobat
>reader and giving the print command on
>windows and some thing similar on
>ubuntu linux.
>
Just let the registered .PDF viewer do
it for you.
>
os.start('myfile.pdf')

Eh? I don't see os.start() it either 2.5
or 2.44 documentation, and it's sure not
there in 2.4.3:

My bad. os.system()

That doesn't work either:

$ ls -l user.pdf
-rw------- 1 grante users 35640
2005-11-21 14:33 user.pdf

$ python
Python 2.4.3 (#1, Dec 10 2006, 22:09:09)
[GCC 3.4.6 (Gentoo 3.4.6-r1,
ssp-3.4.5-1.0, pie-8.7.9)] on linux2 Type
"help", "copyright", "credits" or "license"
for more information.

>>import os
>>os.system('user.pdf')

sh: user.pdf: command not found
32512

Works fine on my system. You linux guys
just have it hard. The op said "windows".
The posting to which you replied specified
Linux.
I can't answer for ubuntu linux but maybe
you can help there?
I don't see how. Pdf files just aren't
executable.

On Windows, this (where fileName is xyz.PDF,
for example): webbrowser.open(r'file://' +
fileName) starts Acrobat Reader with the
document read in. I have no idea why, because
Acrobat Reader sure ain't my browser;)

Maybe someone could try this out on Linux.

Cheers,
Jussi
Feb 13 '07

P: n/a
Dennis Lee Bieber kirjoitti:
On Tue, 13 Feb 2007 08:44:18 GMT, Jussi Salmela
<ti*********@hotmail.comdeclaimed the following in comp.lang.python:

>On Windows, this (where fileName is xyz.PDF, for example):
webbrowser.open(r'file://' + fileName)
starts Acrobat Reader with the document read in. I have no idea why,
because Acrobat Reader sure ain't my browser;)
Most likely Adobe installed the Acrobat plug-in for the browser...
The browser identifies the file as PDF and passes it to the plug-in for
rendering.
I can read PDFs with the browser if I open them in there.

But as I was trying to tell the line above starts the Acrobat Reader EXE
in its own window. The mechanism is probably caused by something I must
have done/installed, but I don't know what.

Anyway: it works just as I want although this may not be the general
behaviour.

(OS: Win XP SP2, Python 2.4.3)
Cheers,
Jussi
Feb 13 '07

P: n/a


For those who care,
the file below should run on a unix/ linux style
system. And "xpdf", amoung others, will run a pdf
file.
import os
def Printpdf():
os.system( 'xpdf form.pdf' )

if __name__ == '__main__' :
Printpdf()

jim-on-linux
On Tue, 13 Feb 2007 08:44:18 GMT, Jussi Salmela

<ti*********@hotmail.comdeclaimed the
following in comp.lang.python:
On Windows, this (where fileName is xyz.PDF,
for example): webbrowser.open(r'file://' +
fileName) starts Acrobat Reader with the
document read in. I have no idea why, because
Acrobat Reader sure ain't my browser;)

Most likely Adobe installed the Acrobat
plug-in for the browser... The browser
identifies the file as PDF and passes it to the
plug-in for rendering.
--
Wulfraed Dennis Lee Bieber KD6MOG
wl*****@ix.netcom.com wu******@bestiaria.com
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support
Staff: we******@bestiaria.com)
HTTP://www.bestiaria.com/
Feb 14 '07

This discussion thread is closed

Replies have been disabled for this discussion.