3
votes

I have installed the library wkhtmltopdf link in my CentOS VPS, when I run it from my server using terminal like : "wkhtmltopdf https://www.google.com google.pdf" I get some messages in the output as following :


Loading pages (1/6) Counting pages (2/6)
Resolving links (4/6)
Loading headers and footers (5/6)
Printing pages (6/6) Done


and the PDF is created.

Now when I use PHP with exec command like :

<?php
$output = shell_exec("/usr/local/bin/wkhtmltopdf https://www.google.com google.pdf");
var_dump($output);
?>

the PDF file is created and all seems ok but I get NULL as value of the $output variable. Why is the exec command not giving the same out put?

Thanks

2
Sorry, while I don't think you understood my answer I was mistaken and it is identical to using shell_exec() so I have removed the answer. I am doing exactly this in my scripts (except I'm using backticks rather than shell_exec) so I'm now completely unsure why it's not working for you. - SteJ
On reading this it occurs to me that maybe shell_exec is disabled in your php.ini? - SteJ
I tried to add "2>&1 &" at the end of the exec script shell_exec("/usr/local/bin/wkhtmltopdf google.com google.pdf 2>&1 &");and it is working now, so any other suggestions please? - t411adyel t411adyel
I'm really confused now -- what exactly are you asking for? All you've done there is redirect the error output to stdout and make the program run in the background? - SteJ
Wait - are you asking the difference between shell_exec and exec? In which case exec does not return the output, it allows you to collect it in an array which you give it as the 2nd perameter...? - SteJ

2 Answers

2
votes

shell_exec and exec are different in that shell_exec's return value is the output, whereas exec only gives you the last line - if you want to retrieve output using exec you need to pass in a 2nd parameter of the array you'd like to store the results in:

<?php
exec("/usr/local/bin/wkhtmltopdf https://www.google.com google.pdf",$output)
var_dump($output)
?>

You can also use backticks instead of shell_exec:

<?php
$output = `/usr/local/bin/wkhtmltopdf https://www.google.com google.pdf`;
var_dump($output);
?>

Please see PHP Execution Operator:

PHP supports one execution operator: backticks (``). Note that these are not single-quotes! PHP will attempt to execute the contents of the backticks as a shell command; the output will be returned (i.e., it won't simply be dumped to output; it can be assigned to a variable). Use of the backtick operator is identical to shell_exec().

Additional:

Your code is correct, but the script is probably timing out before wkhtmltopdf is finished, meaning it never receives the return value. Try this to extend php's timeout and give it time to finish it's wkhtmltopdf process:

<?php
    set_time_limit(60); // 60 seconds should be long enough to create your pdf
    $output = `/usr/local/bin/wkhtmltopdf https://www.google.com google.pdf`;
    var_dump($output);
?>
1
votes

You should probably echo the output if you don't want to see a NULL result:

$output = shell_exec("/usr/local/bin/wkhtmltopdf https://www.google.com google.pdf 2>&1");
echo($output);

Since the output formatting looks bad it can also be cleaned up:

$output = shell_exec("/usr/local/bin/wkhtmltopdf https://www.google.com google.pdf 2>&1");
$tidyit = preg_replace("/(.[^\[]*\s\d{1,2}%)\s(?<!100%)|([=>\[]*)/","",$output);
$result = preg_replace("/]/","<br>",$tidyit);
echo($result);

Result:

Loading pages (1/6) 100%
Counting pages (2/6)
Object 1 of 1 Resolving links (4/6)
Object 1 of 1 Loading headers and footers (5/6)
Printing pages (6/6)
Preparing
Page 1 of 1 Done