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
How to Iterate Over Non-English File Names in PHP
Get Facebook Meta Tags with PHP
Concatenating a String and Primary Key Id While Inserting
How to Add a New Method to a PHP Object on the Fly
Casperjs Passing Data Back to PHP
PHP Warning Permission Denied (13) on Session_Start()
How to Solve General Error: 2006 MySQL Server Has Gone Away
How to Echo Out Table Rows from the Db (Php)
Laravel Route Issues with Route Order in Web.Php
Get the Maximum Value from an Element in a Multidimensional Array
Avoiding Form Resubmit in PHP When Pressing F5
Strange Behaviour with Numbers That Have a Leading Zero
Preg_Match() Unknown Modifier '[' Help
Curl Code in PHP Dumps Output to the Page
Checking If Your Code Is Running on 64-Bit PHP
How to Send a Status Code in PHP, Without Maintaining an Array of Status Names
Set an Environment Variable in .Htaccess and Retrieve It in PHP