tags:

views:

93

answers:

2

I'm currently building a class in PHP that generates PDF documents using the WKHTMLTOPDF command line app.

To do this, I'm using a call to shell_exec to call the WKHTMLTOPDF executable. However, this particular call does not seem to be executed; it returns NULL almost instantly.

A small test I did gave me the following results:

var_dump(shell_exec('ping nu.nl'));
// This prints a string, containting the expected output of the ping command
var_dump(shell_exec('"c:/wkhtmltopdf/wkhtmltopdf.exe" --orientation "Landscape" --page-size "A2" --margin-top "25mm" --margin-left "20mm" --margin-bottom "20mm" --margin-right "20mm" "http://www.nu.nl/" "C:/Temp/1280310218.pdf"'));
// This prints NULL

So, shell_exec() seems to be working, also safe_mode is off, and pasting the full command into cmd.exe does run the command properly.

If the problem isn't in either of the above (safe mode, a faulty command, or shell_exec() itself) what else can it be? All I can think about is a user rights issue, but both the executable and the directory it's in have full access settings for every user group on my system.

(Note: Though I'm developing on a Windows machine, this code will run on a Linux server in production. Hence, windows-only solutions aren't what I'm looking for, unless ofcourse this problem itself turns out to be related to windows)

A: 

You could use a php library to do that, which would be more efficient and integrated. There is for example html2pdf.

Guillaume Lebourgeois
The reason I chose a CLI app, and not a PHP library, is because none of the ones I've checked offers us all the options we need; FPDF, DOMPDF, TCPDF among others, all were lacking in one way or another. WKHTMLTOPDF is by far the best and most complete solution. If it works, that is.
Duroth
A: 

Check permissions by create file using fopen() in temp dir. Does it create pdf ? Maybe this program doesn't output to stdout, but stderr same as ffmpeg for example. Try adding "2>&1" after your command.

Or, this software uses some files in directory where is installed. Try first to change directory by adding something like "cd c:\wkhtmltopdf;YOUR_COMMAND"

killer_PL
Your first suggestion did the trick; Appearantly, quotes around the command to be executed (`"c:/wkhtmltopdf/wkhtmltopdf.exe"`) are not allowed, and that error was indeed stored in stderr. And to think I only added those quotes because the app was installed in `Program Files/` in the first place!
Duroth