Hi,
I am learning python CGI programming but got stuck with some issues. I would really appreciate if you could help me out.
Problem: I have made a web page using Python CGI scripts; from one of the menu items on the web page, I am trying to call another python cgi script which opens a pdf file. Following is the code:
In index.py

#!C:\Python25\python.exe
import cgi
import cgitb; cgitb.enable()
print "Content-type: text/html\n"
<html><head> <title>My page</title>
<link rel="stylesheet" href="C:\Program Files\Apache Software Foundation\Apache2.2\cgi-bin\mystyle.css"/></head>
<body><ul class="glossymenu"><li class="current">
<a href="resume.py"><b>Resume(.pdf 25kb)</b></a></li></ul></body></html>

In resume.py:

#!C:\Python25\python.exe
import cgi,os
import cgitb
cgitb.enable()
print "Content-type: text/html\n"
os.system('c:\resume.pdf')

When I execute http://localhost/cgi-bin/index.py the web page is displayed with 'resume' as the menu item.
When i click on menu 'resume' the script resume.py is executed but the pdf file is not opening. 'Done' is reflected on the bottom left corner of the browser & a white page is displayed.
I also noticed that when I click the 'resume' menu item at the same time a 'AcroRd32.exe' process starts in the system processes but no pdf file is displayed nor any error is thrown.
Further when I execute the script resume.py from the command line the pdf file opens up but not from browser.

Could you please throw some light on the issue?

Thanks & Regards

## All 8 Replies

I have never written a line of Python code in my life. So I am superbly qualified to answer this post.

But I am guessing the os.system call just calls the operating system to execute the file supplied as the argument. So it won't return anything to the browser.

But you don't need Python for this. You can replace all this with the following HTML code.

<html><head></head>
<body>
<a href="resume.pdf">Resume(.pdf 25kb)</a>
</body></html>

Hi

Thanks for your reply!!

I am trying it with python just to get my hands on with pyhton CGI.

Regarding the os.system call: The pdf file opens up when i execute the resume.py from command line or double click it.So I dont think that should be the issue.

I had tried a similar approach as well using HTML code but the error I get is :
[error] [client 127.0.0.1] (9)Bad file descriptor: don't know how to spawn child process: C:/Program Files/Apache Software Foundation/Apache2.2/cgi-bin/resume.pdf, referer: http://localhost/cgi-bin/index.py

Thanks

Hello,

I was just curious if you could finally get the solution to this problem which you have referred to as I too have just started to get my hands over Python CGI programming.

I guess, being so long, by now you might have overcome this problem.
Looking forward to a positive response and thanking you in advance.

Just an educated guess, but you would have os.system call the pdf browser with the file name (but this is on Linux).
os.system('Acrobat c:\resume.pdf')

Hi,

It still didn't work :( Now i've started feeling that it's a windows problem.
This is what the latest version of my script looks like:

#! C:\Python25\python.exe
import os.path, cgi
import cgitb
cgitb.enable()
file_path = "c:/Broucher.pdf"
print "Content-Type: application/pdf"
print "Content-Transfer-Encoding: binary"
#print "Content-Length: %s" % (os.path.getsize(file_path) + os.path.getsize(file_path))
#print "Content-Disposition: attachment; filename=%s" % os.path.split(file_path)[1]
print
print open(file_path).read(),


When I run this script it says PDF file is corrupt. Although the file is perfectly fine.
Do let me know if you find out a solution.

Thanks

To open it, as opposed to viewing in Acrobat, you will likely have to open as a binary file
print open(file_path, "rb")
There is pypdf, and probably others, for working with a pdf file in python. You would also probably use PIL (Python Imaging Library) if there are embedded images in the pdf file.

You may have to use a double backslash, as I'm on Linux I don't know, so just use .join if there are problems.
file_path = os.path.join("c:", "Broucher.pdf")
And are you sure it is not "Brochure"? HTH.

file_path = os.path.join("c:", "Broucher.pdf")

For whatever reason, on Windows os.path.join doesn't play nice with the drive letter:

>>> import os
>>> os.path.join('c:', 'foo', 'bar')
'c:foo\\bar'
>>> os.path.join('c:\\', 'foo', 'bar')
'c:\\foo\\bar'
>>> os.path.join(r'c:', 'foo', 'bar')
'c:foo\\bar'
>>>

I too am trying to write a similar program that I would be using later to integrate with CGI, so that the work that is done by the script through a command prompt can be done directly through the browser, just by the click of a button (or similar action).

I have some sort of bug in my initial script itself, which gives me the following error. The Script and the error are as follows.

SCRIPT-->
#!/usr/bin/python

import cgi, string, os, sys, cgitb, commands, subprocess
import posixpath, macpath
comd = [\
"tar -xf x.tar.gz", \
"cd demo", \
"cp README ../", \
]
outFile = os.path.join(os.curdir, "output.log")
outptr = file(outFile, "w")
errFile = os.path.join(os.curdir, "error.log")
errptr = file(errFile, "w")
retval = subprocess.call(comd, 0, None, None, outptr, errptr)
errptr.close()
outptr.close()
if not retval == 0:
errptr = file(errFile, "r")
errData = errptr.read()
errptr.close()
raise Exception("Error executing command: " + repr(errData))

ERROR-->

Traceback (most recent call last):
File "process.py", line 18, in <module>
retval = subprocess.call(comd, 0, None, None, outptr, errptr)
File "/usr/lib/python2.5/subprocess.py", line 443, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib/python2.5/subprocess.py", line 593, in __init__
errread, errwrite)
File "/usr/lib/python2.5/subprocess.py", line 1135, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory

I would be thankful to you all for any suggestions in this regard and also for some precautions that I may require to get this working via the browser too.

Regards.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.