PotoSolutions
asked on
How to run a remote exe using php
Hi,
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):
doc.pdf
out.fdf
pdftk.exe
And created a PHP script with the following:
<?php
make_pdf();
function make_pdf()
{
// Send a force download header to the browser with a file MIME type
header("Content-Type: application/force-download ");
header("Content-Dispositio n: 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?
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):
doc.pdf
out.fdf
pdftk.exe
And created a PHP script with the following:
<?php
make_pdf();
function make_pdf()
{
// Send a force download header to the browser with a file MIME type
header("Content-Type: application/force-download
header("Content-Dispositio
// 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?
ASKER
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?
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.
That's all I have for now.
ASKER
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...
$last_line = shell_exec('/usr/bin/pdftk
But it still hangs... I wonder if there is a way to get interactive output using PHP...
ASKER
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 '
</pre>
<hr />Last line of the output: ' . $last_line . '
<hr />Return value: ' . $retval;
Now I'm getting:
1896
-------------------------- ---------- ---------- ---------- ---------- ---------- ----
Last line of the output: 1896
-------------------------- ---------- ---------- ---------- ---------- ---------- ----
Return value: 0
And a 0KB empty PDF document... how frustraiting! I'm running out of ideas!
$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 '
</pre>
<hr />Last line of the output: ' . $last_line . '
<hr />Return value: ' . $retval;
Now I'm getting:
1896
--------------------------
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');
passthru('/usr/bin/pdftk doc.pdf fill_form n.fdf output out.pdf flatten dont_ask 2>&1');
ASKER
Thanks... I just tried it but it still hangs in the same way and produces a 0KB file.
I did find this... https://www.experts-exchange.com/questions/24247277/pdftk-shell-script-works-fine-when-run-from-command-line-but-not-when-invoked-by-PHP.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;
exit;
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:
passthru('hello');exit;
No output at all.... I thinking that if I can get the C program working then I'm virtually there?
I did find this... https://www.experts-exchange.com/questions/24247277/pdftk-shell-script-works-fine-when-run-from-command-line-but-not-when-invoked-by-PHP.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;
exit;
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:
passthru('hello');exit;
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.
ASKER
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: https://www.experts-exchange.com/questions/24247277/pdftk-shell-script-works-fine-when-run-from-command-line-but-not-when-invoked-by-PHP.html)
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: https://www.experts-exchange.com/questions/24247277/pdftk-shell-script-works-fine-when-run-from-command-line-but-not-when-invoked-by-PHP.html)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Bingo!!!
passthru('/home/c/o/countr y/web/publ ic_html/pd f/hello'); exit;
Output: Hello World!
Thanks!
Now I have enough info to try unblock.c again... fingers crossed. I'll give it a go and let you know.
passthru('/home/c/o/countr
Output: Hello World!
Thanks!
Now I have enough info to try unblock.c again... fingers crossed. I'll give it a go and let you know.
ASKER
It worked!
passthru('/home/c/o/countr y/web/publ ic_html/pd f/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.
passthru('/home/c/o/countr
Thanks very very much for your help. Getting the unblock program to work was the key to stopping the command from hanging.
pdftk also comes in a Linux version. You may need that instead if the server uses the Linux OS.