Skip to content
August 16, 2009 / ffstefan

PDFTK PHP Problem, Hang without errors

Problem: Merging (flattening) a PDF using PDFTK on PHP / APACHE  does not work most of the time. Sometimes it does. PHP hangs, no errors.

Solution: Dirty workarround. Replaced the PHP function call to pdftk with a virtual() or get_file_contents() call to a Perl Script, which executes pdftk.

Details: This drove me nuts for 2 days. For PDF generation – filling MySQL data into a PDF form – I use PDFTK-PHP by Andrew Heiss. I can call my script from PHP command line (CLI) on the server. This always works, no errors, never. Calling the same script with the same data on PHP (mod_php, Apache2), it will fail most of the time – PHP simply hangs. No errors in any logs. Doing a ps -ax call on the server, I see that the pdftk process hangs. As soon as I kill it manually with kill -9 [PID], the PHP script continues.

Andrew Heiss uses passthru for the pdftk call. Experimental switching to any other execution method like proc_open(), system() or shell_exec() had no success. Hangs again. But trying the very same pdftk call in perl suddenly works. So the dirty method is to call a perl script from within PHP. I wrote a little perl script which takes parameters from pdftk-php and executes pdftk.

Boom: PHP hangs again when I call the perl script using any php execution command like passthru() or system(). So the final and dirty workarround is to call the perl script by doing another http request using get_file_contents() or fopen(). Drawbacks: another http request, temporary file creation.

Note: pdftk-php originally calls pdftk with output param set to STDOUT and passes data directly to the browser. Of course I needed to patch this by having the perl helper script generate a temporary pdf file.

Conclusion: Must be related to process forking or memory mapping in Apache2 and / or PHP. Submitted bug report to PHP.

Server Config: Linux 2.6.19 (gentoo), Apache 2.2.11 worker MPM, PHP 5.2.10, pdftk 1.41



Leave a Comment
  1. Brad Baumann / Aug 24 2009 9:38 pm

    I’ve had this same problem too, running Apache in Windows 2003 Server. Going to try calling a batch file rather than exec()

  2. Matt / Apr 6 2010 1:02 pm

    Same problem here:

    Suse Linux 10.
    Apache 2.2.14
    PHP 5.3.0
    Pdftk 1.41

  3. Michael / Aug 21 2010 3:11 pm

    Just in case anyone is having the same problem and needs more help, here’s the perl wrapper I wrote, based on this excellent blog post:

    my $inputfile = $ENV{“QUERY_STRING”};
    my $tempfile = `mktemp`;
    system (“pdftk input.pdf fill_form $inputfile output $tempfile”);
    print (“Content-type: application/pdf\n\n”);
    system (“cat $tempfile”);
    unlink $tempfile;

    Put perl script from your cgi-bin directory, turn on mod_cgi or mod_cgid if not already enabled in your apache config, and call from PHP script like this:

    $tempfdf = tempnam(“/tmp”, “fdf”);
    $handle = fopen($tempfdf, “w”);
    fwrite ($handle, createFDF($file, $info));
    fclose ($handle);
    unlink ($tempfdf);

    I used the CreateFDF script from Justin Koivisto

    • Ryan Dagey / Aug 21 2010 6:59 pm

      I have the same problem (with PDF concatenation via pdftk) on Gentoo, Apache 2/PHP 5. I filed a Gentoo forum post here, but have not gotten any feedback:

      I am very comfortable with Apache/PHP, but not Perl. I want to do concatenation with pdftk, so it is not clear to me how to modify the Perl script to allow this. Could you offer any suggestions as to how the above perl script could be modified to allow concatenation via pdftk from a PHP page on the webserver? In particular, the number of individual PDF files that will be concatenated is dynamic, could be any number of individual pages. Thanks for any help/suggestions you can offer.

  4. Ryan Dagey / Aug 31 2010 7:12 pm

    I was able to to get a perl workaround up and working, do you have a link to the PHP bug that was filed? I ask because this problem still exists and I’d like to see what progress has been made on the bug.

  5. Sid Steward / Nov 10 2010 6:26 pm

    Based on this and other discussions, I have added a workaround to pdftk 1.43 that should resolve this problem. Please let me know how this works for you.


    Sid Steward
    Pdftk Author

    • Michael / Nov 15 2010 1:44 pm

      Thanks very much Sid – I’ve just compiled and installed pdftk-1.44 and it now works directly from PHP, no perl wrapper required. 🙂

  6. dpelletier / Nov 11 2012 8:55 pm

    Issue still presents itself occasionally. Windows, pdftk 1.44
    Running from command prompt in windows.

    I wish pdftk would have the ability to simply burst by bookmarks (like 1st level ~ to produce chapters).

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: