Link to home
Start Free TrialLog in
Avatar of chennipher
chennipher

asked on

pdftk shell script works fine when run from command line, but not when invoked by PHP


I'm running pdftk, a program that deals with PDFs, on a shared server running Linux. Among its operations, it can rotate a pdf 180 degrees and fill a pdf form with .fdf data.

I've installed pdftk and have been able to run operations from the command line without any problem. I'm merging .fdf ata with a pdf form I've created and outputting to a folder on the server. This works great when run from the command line.

However, when I invoke pdftk through a php file in the browser to merge the .fdf data with the pdf, using the exact same script, the process hangs and eventually produces a 0KB pdf file instead of the ~28KB pdf produced when run from the command line. I let the program run its course and the checked my site's error log. Here is the error this
attempt produced:

[Thu Mar 19 16:42:26 2009] [error] [client 24.61.106.202] sh: line 1:  8951 Killed  /home/sweetbel/bin/pdftk-1.41/pdftk/pdftk recipe_card_01.pdf fill_form test.fdf output /home/sweetbel/public_html/pdf/recipe_test.pdf, referer: http://sweetbellpaper.com/pdf/

Code from my php file, with full and correct paths:
passthru( '/home/sweetbel/bin/pdftk-1.41/pdftk/pdftk recipe_card_01.pdf fill_form test.fdf output /home/sweetbel/public_html/pdf/recipe_test.pdf' );

Here's the funny thing ... my php file invoking pdftk works just fine when I'm using the "rotate pdf 180 degrees" operation (/home/sweetbel/bin/pdftk-1.41/pdftk/pdftk recipe_card_01.pdf cat 1-endS output /home/sweetbel/public_html/pdf/recipe_test.pdf). So it seems the conflict is with the fill_form operation itself.

Does anyone have a clue as to why this is happening?

Thanks,
Jennifer
passthru( '/home/sweetbel/bin/pdftk-1.41/pdftk/pdftk recipe_card_01.pdf fill_form test.fdf output /home/sweetbel/public_html/pdf/recipe_test.pdf' );

Open in new window

Avatar of Karl Heinz Kremer
Karl Heinz Kremer
Flag of United States of America image

Does the script - when you run it from the command line - produce any output? Does it expect any input from you? PHP is not one of my strong areas, so I don't know if there is anything special you would need to do on the PHP side.

Here is what I would try: Create a shell script that contains the pdftk command with all command line options. Then start that shell script from PHP. Does that work?
Avatar of chennipher
chennipher

ASKER

Yes, the script produces a PDF exactly as expected when run from the command line. Pdftk is working great for me as long as I'm running it from the command line.

No, the shell script called from PHP does not work. However, the PHP script called from shell DOES, go figure. It seems that there's some issue with pdftk's fill_form operation being called from the browser. Everything else in pdftk works fine from the browser. Very strange.
I'll run some tests.
One more: Which version of pdftk are you running?
I'm running pdftk 1.41. I've attached a simple jpeg illustration which probably illustrates my problem better than my poor vocabulary!
sweetbell-troubleshoot.jpg
You are calling passthru(), but you are writing a file. Are you then (later) initiating a download in your PHP code? Why are you then not just using a system() or exec() command?

If your intention is to actually take the output of the pdftk command and "pass it through" to the browser, you need to replace the output filename with a dash (e.g. pdftk file.pdf fill_form test.fdf output - )

Well, I've tried system, exec and shell_exec and they all behave the same as passthru with this particular command ... 0KB PDF on the server. I've also tried serving the output PDF directly to the browser with "output -" but it also hangs at "Creating Output."
I've tried it on my system, and it works. I do end up with the merged PDF being served out with "output -". Have you tried it with a different PDF/FDF combination? Maybe there is something wrong with one of these files.
ASKER CERTIFIED SOLUTION
Avatar of chennipher
chennipher

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Good. So it may also play a role on which OS you try to do that - I was using Mac OS.  Post a request to have this question PAQ'ed and your points refunded.
The above code works in CentOS 5 but was missing this include:

#include <unistd.h>

It compiled easily using:

g++ -o unblock unblock.c