How to Get Wkhtmltopdf to Execute via PHP

Execute wkhtmltopdf from PHP

wkhtmltopdf has bindings, one of them is for PHP. You could give those a shot.

How to execute WKHTMLTOPDF from PHP?

You should't be putting the quotes around the library.You can catch the output of the command this way:

$commandString = '../wkhtmltopdf/wkhtmltopdf-i386 http://www.estiroad.com/export.php file.pdf 2>&1';
$output = shell_exec($commandString);

The 2>&1 in UNIX will mean that the output will come through. 1 is stdout. 2 is stderr.
Hope this helps.

Or in windows

$commandString = '../wkhtmltopdf/wkhtmltopdf.exe http://www.estiroad.com/export.php file.pdf 2> output';
print $out ? $out : join("", file("output"));

From the permission issue it looks like you're running the production script on linux. Go to your production server and run

$ uname -a

You'll get something like:

Linux ora100 2.6.5-7.252-smp #1 SMP Tue Feb 14 11:11:04 UTC 2006 x86_64 x86_64 x86_64 GNU/Linux

the x86_64 suggest you're running a 64 bit CPU, if that's the case download the amd64 version of binary, otherwise download the i386 one. Both can be obrained from this url: http://code.google.com/p/wkhtmltopdf/downloads/list

Keep the windows binary. Have you got a config file? if you do make sure you have a switch where you assign your library path to a constant based on your environment.

if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
// this is windows server
define('WKHTML_LIB', "../wkhtmltopdf/wkhtmltopdf.exe");
} else {
// or the 64 bit binary?
define('WKHTML_LIB', "../wkhtmltopdf/wkhtmltopdf-i386");
}

Then change your code that initiates wkhtmltopdf:

$commandString = WKHTML_LIB' http://www.estiroad.com/export.php file.pdf 2> output';
print $out ? $out : join("", file("output"));

How to execute WkHtmlToPdf commands in PHP?

PHP provides multiple methods for running an external command-line string.

  • The backtick operator
  • shell_exec()
  • system()
  • exec()
  • passthru()

Which one you should use depends on exactly what you want to do -- ie whether you need to get a result back from the command to your PHP code or not; whether you want to output the result to the browser; whether you want the PHP program to wait for the external code to finish or not before proceeding...

You'll need to read the manual pages linked above to decide which of the options is best suited to your needs.

Hope that helps.

Executing wkhtmltopdf from PHP fails

Add 2>&1 to the end of your command to understand the exact error.

exec('wkhtmltopdf http://somesite.com /home/user/file.pdf 2>&1');

This way it will tell you the error when running this under Apache. Once you know what's wrong, it is easier to address. I had a similar problem before with ImageMagick

How to run wkhtmltopdf from php in shell

Agree that this can be an access right issue.

Try to save to another folder/file:

Desktop/test.pdf



Related Topics



Leave a reply



Submit