cfreak has asked for the wisdom of the Perl Monks concerning the following question:

AHHHHHHHHHHHHHHHHHH ... okay now that I have that out of the way :). I'm trying to convert MS Word documents uploaded by a client to PDF files. First I used Antiword to convert the Word doc to a Postscript file and then tried using ps2pdf to take the Postscript and turn it into the PDF. This setup works perfectly from the command line but when I try running it as a CGI script the ps2pdf command chokes and doesn't really give much to go on. When I've called it directly I get error 127. Which I understand is often "Command not found" at least in the shell, but the command certainly exists so I suspect that for ps2pdf it means something else (though I've been unable to locate any documentation on that). If I envoke a shell to run it from within Perl I get an exit value of 2. Which I've read to be "Invalid use of shell builtins" (huh?). So I'm very confused. Has anyone run into this problem before. Is there a different program I could use to convert the Postscript to PDF? (I tried using wvPDF but the output was not correct).

Anyway, here's some code:

use strict; use CGI::Carp 'fatalsToBrowser'; print "Content-Type: text/plain\n\n"; # Change to the directory where I want the final output # stored (this seems to help antiword work correctly) chdir('/path/to/my/pdfs') or die "Couldn't change directory: $!\n"; # Antiword only outputs to STDOUT so I just decided to use # backticks to capture the output. In the real program I # check and make sure no one is doing anything funny with # the filename my $postscript = `/usr/local/bin/antiword -m 8859-1.txt -p letter /tmp +/worddoc.doc`; # since more than one person could be working on this at # a single time make sure we have a unique name for the # temp file my $tmpfile = ""; my $count = 0; { $tmpfile = "pdftmp$count.ps"; $count++; redo if -e "/tmp/$tmpfile"; } # save the postscript to the temp file open(TMPFILE,">/tmp/$tmpfile") or die "Couldn't open $tmpfile: $!\n"; print TMPFILE $postscript; close(TMPFILE); # Convert the Postscript to pdf (here's where we choke) system("/usr/local/bin/ps2pdf","/tmp/$tmpfile","test.pdf") == 0 or die + $? >> 8; print "Done\n";

UPDATE: the problem was a difference in the PATH between the shell environment and the CGI environment. Abigail-II is my hero :). Next time I'll get more sleep before coding

Replies are listed 'Best First'.
Re: Help Converting Postscript to PDF in a CGI script
by Abigail-II (Bishop) on Jan 12, 2004 at 15:16 UTC
    As one can read from the manual page, ps2pdf makes use of ghostscript to do the actual conversion. My guess is that the default PATH that's set by the HTTP server doesn't include the directory ghostscript is in.

    Try setting your $ENV {PATH} variable to an appropriate value before issuing the system command.

    Abigail

Re: Help Converting Postscript to PDF in a CGI script
by duff (Parson) on Jan 12, 2004 at 15:15 UTC

    Rather than specifying the full path to each of your executables, you could explicitly set $ENV{PATH}. If ps2pdf doesn't really live in /usr/local this would actually make your program suddenly work :-) You should be running with taint mode turned on (#!/usr/bin/perl -T) anyway which would force you to be explicit about your path.

    I'm obviously just guessing about your path being wrong. Something else that might be a factor is that your server may be setup such that CGIs run in a restricted environment which doesn't have access to /usr/local (for instance).

    Hope this helps,