Search code examples
phppdftk

Execution of PDFTk from PHP script is failing with error code 11


I am trying to Fill in a PDF from PHP script. I am following the article by Sid Steward at the following URL.

I have configured PDFTk package on CentOS linux distribution and I am able to execute the pdftk from the command prompt and it merges the FDF form with the PDF and successfully generates the flattened(Filled) PDF. I am using following command to test the Pdftk using shell.

pdftk /tmp/form.pdf fill_form /tmp/fdfbm0pe7 output /tmp/filledform.pdf flatten

But When try to execute a similar command through PHP, I am getting the error. The passthru command is failing with error code 11. Following is the php code I am using to execute the command:

$command = 'pdftk form.pdf fill_form '. $fdf_fn. ' output - flatten';
passthru($command, $error);

The $fdf_fn above has the FDF file name. The form.pdf is the fill-able pdf form. Both the form.pdf and the PHP script file from which I have given the above lines of code are in the same folder. I have checked that PDFtk is executing correctly through PHP by echoing shell_exec('pdftk') and it was returning the standard help details.

Just to provide more details, the path to pdftk is /usr/bin/ and PHP script and PDF form files are located under /var/www/html/pdfmerge.

Can some one please guide what I am doing wrong that the command execution through PHP is failing with error code 11?


Solution

  • I had the exact same issue with PHP running PDFTK on CentOS and spent a number of hours hunting things down, so I hope someone will find this useful. My specific use for PDFTK is for the FillPDF module on Drupal. The solution to the exit code 11 issue was setting a local policy module to allow httpd to run PDFTK. If you grep /var/log/audit/audit.log for pdftk like this

    grep -i pdftk /var/log/audit/audit.log
    

    you should see some errors, probably one for 'execmem', amongst others. Here is what I did to fix the issue...

    Running this command will pull the errors out of the log and put them into a file for you to review.

    grep -i pdftk /var/log/audit/audit.log | audit2allow -m pdftklocal > pdftklocal.te
    

    If the output looks good (e.g. there IS output), run this command to create the module.

    grep -i pdftk /var/log/audit/audit.log | audit2allow -M pdftklocal
    

    Once the module is created, you make the policy active by running this command.

    /usr/sbin/semodule -i pdftklocal.pp
    

    PDFTK should run now from PHP. If it does not, check to see if httpd_enable_cgi is on by running

    getsebool httpd_enable_cgi
    

    If it is not on, turn it on by running

    setsebool -P httpd_enable_cgi 1
    

    Details on the policy module can be found here: http://wiki.centos.org/HowTos/SELinux#head-faa96b3fdd922004cdb988c1989e56191c257c01