Hi, I am having the problem of running ps2pdf from a python script.

From the command line, ps2pdf works just fine. However, when I tried the following code:

os.chdir("/tmp/test") # file.ps resides in this dir
os.system("ps2pdf file.ps file.pdf") 

The script generated an empty file.pdf with no pages.
Does anyone know why?
Thanks.

Edited 3 Years Ago by Dani: Formatting fixed

The module subprocess is new in Python24 and is much more reliable than os.system(). You may have to give the full path of file "ps2pdf" and file "file.ps" (eg. "/tmp/test/file.ps").

import subprocess
subprocess.call(["ps2pdf", "file.ps", "file.pdf"])

Note that subprocess.call() takes a list.

The module subprocess is new in Python24 and is much more reliable than os.system(). You may have to give the full path of file "ps2pdf" and file "file.ps" (eg. "/tmp/test/file.ps").

import subprocess
subprocess.call(["ps2pdf", "file.ps", "file.pdf"])

Note that subprocess.call() takes a list.

I just tried subprocess.call. It generated the same empty pdf file.

I guess the problem has sth to do with "ps2pdf", which is a command script (batch file) rather than a single binary executable file?

Can you show us the batch file?

Here is the content of /usr/bin/pse2pdf:

#!/bin/sh
# $Id: ps2pdf,v 1.1 2000/03/09 08:40:40 lpd Exp $
# Convert PostScript to PDF.
# Currently, we produce PDF 1.2 by default, but this is not guaranteed
# not to change in the future.
exec ps2pdf12 "$@"

One more thing:
the above code snippet is just part of a small python cgi script (test.cgi).
Case 1: If I ran it from shell:
python test.cgi
A good file.pdf was generated.

Case 2: If the test.cgi was invoked from the corresponding webpage, then a file.pdf with no pages was generated.

I guess the only difference between the two cases is the user. In case 1, a local user (root) invoked test.cgi while , in case 2, a web user (apache) did it. However, I still have no idea how to fix the problem.

Any comment will be appreciated!

Edited 3 Years Ago by Dani: Formatting fixed

Hi gbs,

It is possible that (a) there are two versions of ps2pdf installed on your system, and the apache user uses one and the root user uses the other, or the more likely case, (b) something in the root environment which is necessary for ps2pdf's proper function is missing in the apache environment.

Have you tried to switch to the apache user, then run ps2pdf at the command line? Barring that, you could try having your web script print the environment variables (this would be the apache user's environment) and compare them to the root user's environment variables.

This question has already been answered. Start a new discussion instead.