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

K

krishnakant Mane

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.
 
G

Geoff Hill

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.
 
K

krishnakant Mane

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
 
L

Larry Bates

krishnakant said:
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
 
G

Grant Edwards

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. 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?
 
L

Larry Bates

Grant said:
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.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
 
G

Grant Edwards

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. sh: user.pdf: command not found
32512
 
P

Patricio Olivares

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.
 
L

Larry Bates

Grant said:
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.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
 
G

Grant Edwards

Grant said:
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.
 
J

Jussi Salmela

Grant Edwards kirjoitti:
Grant said:
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
 
P

peter

Grant Edwards kirjoitti:
Grant Edwards wrote:
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 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.
 
X

XBello

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
 
A

Antoon Pardon

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)
 
J

jim-on-linux

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







Grant Edwards kirjoitti:
Grant Edwards wrote:
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
 
D

Dennis Lee Bieber

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
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
J

Jussi Salmela

Dennis Lee Bieber kirjoitti:
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
 
J

jim-on-linux

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
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top