We help IT Professionals succeed at work.

How to run a remote exe using php

PotoSolutions asked

I need to be able to fill in fields within a precreated PDF document using PHP. I have worked out how to do this from the command line using info from this page: http://stackoverflow.com/questions/77873/filling-pdf-forms-with-php

The command line I am using is:

pdftk doc.pdf fill_form out.fdf output out.pdf flatten

This executes pdftk.exe and takes my document and fills it in using the info stored in out.fdf. The resulting PDF is saved as out.pdf.

The next step was to get it working using PHP. I uploaded the following files to my remote server (into one folder):


And created a PHP script with the following:


function make_pdf()
      // Send a force download header to the browser with a file MIME type
      header("Content-Type: application/force-download");
      header("Content-Disposition: attachment; filename=\"out.pdf\"");
      // Actually make the PDF by running pdftk - make sure the path to pdftk is correct
      // The PDF will be output directly to the browser - apart from the original PDF file, no actual PDF wil be saved on the server.
      passthru("pdftk doc.pdf fill_form out.fdf output - flatten");

Ran the script... no output at all. Just a blank page. I also tried using exec() and shell_exec(). No joy apart from a return code of 127...

I checked that the exe file was executable (chmod 755). Still no joy... Does the exe have to be placed somewhere specific on the server?
Watch Question

Are you sure the remote machine runs the Windows OS. You will need the Windows OS to run Windows programs.

pdftk also comes in a Linux version. You may need that instead if the server uses the Linux OS.


You are right. I thought of that after I posted the question so I called the hosting company and asked them to run aptget on the Ubuntu server to install the pdftk package.

So now I'm using...

/usr/bin/pdftk doc.pdf fill_form out.fdf output out.pdf flatten

But the php script just "hangs" when I run it... and doesn't seem to generate any output... any idea?

Sorry no, if you have SSH access you could try running it from the command-line that may give you a better idea of what is going on. Also check the return code from passthru function (second argument). This could provide a hint.

That's all I have for now.


Unfortunately I don't have SSH access. I thought maybe it was hanging due to it requiring some input, so I tried this:

$last_line = shell_exec('/usr/bin/pdftk doc.pdf fill_form n.fdf output out.pdf flatten dont_ask > out.txt');

But it still hangs... I wonder if there is a way to get interactive output using PHP...


Another try...

$last_line = system('nohup /usr/bin/pdftk doc.pdf fill_form n.fdf output out.pdf flatten dont_ask > /dev/null & echo $!;', $retval);

// Printing additional info
echo '
<hr />Last line of the output: ' . $last_line . '
<hr />Return value: ' . $retval;

Now I'm getting:


Last line of the output: 1896
Return value: 0

And a 0KB empty PDF document... how frustraiting! I'm running out of ideas!

try capturing the stderr stream.

passthru('/usr/bin/pdftk doc.pdf fill_form n.fdf output out.pdf flatten dont_ask 2>&1');


Thanks... I just tried it but it still hangs in the same way and produces a 0KB file.

I did find this... http://www.experts-exchange.com/Web_Development/Document_Imaging/Adobe_Acrobat/Q_24247277.html

It looks like it must be something to do with signals. The only problem is that I don't have SSH access to the server to compile the C program. So I've been trying to do it using PHP (yes... i know!)...

I uploaded the C script and ran:

passthru('gcc -o unblock unblock.c');

That generated the "unblock" executable (10kb) and I set it to execute chmod 775. Then I tried this:

passthru('unblock /usr/bin/pdftk doc.pdf fill_form n.fdf output out.pdf flatten dont_ask', $retval);
print $retval;

The output was "127" and no out.pdf file created at all.

I have a feeling the problem is the 'unblock' command, so I created this C program:

#include <stdio.h>
int main(void) {
  printf("Hello World!\n");
  return 0;

Compiled it, then ran:


No output at all.... I thinking that if I can get the C program working then I'm virtually there?

Make also sure you are allowed to access the program from PHP. Check the open_basedir setting on the server.


I'm not sure how to check that, but I'm sure I can run the program, because the following works fine:

passthru('/usr/bin/pdftk doc.pdf generate_fdf output n.fdf');

I used that line to generate the n.fdf file from the pdf.

Any idea how why the basic C program above didn't work? I feel this is the key... if I can get unblock.c working... (see bottom of this thread: http://www.experts-exchange.com/Web_Development/Document_Imaging/Adobe_Acrobat/Q_24247277.html)
You can check the open_basedir setting from phpinfo()

For basic C program might not be in an allowed path. Your server may be configured to not just allow running programs from just any location. The hello program might not be in the search path (in which case you need an absolute path to execute it).




Output: Hello World!


Now I have enough info to try unblock.c again... fingers crossed. I'll give it a go and let you know.


It worked!

passthru('/home/c/o/country/web/public_html/pdf/unblock /usr/bin/pdftk doc.pdf fill_form out.fdf output out.pdf flatten dont_ask', $retval);

Thanks very very much for your help. Getting the unblock program to work was the key to stopping the command from hanging.