11

I have this working fine from Linux command line:

wkhtmltopdf entry.html output.pdf

But the following doesn't work from PHP code:

exec ('wkhtmltopdf entry.html output.pdf');

Interesting, I've googled and a lot of non-checked solutions and with no explanation why this is a problem. Thanks if you have the good ones.

Joel Peltonen
  • 13,025
  • 6
  • 64
  • 100
Ginger Opariti
  • 1,282
  • 5
  • 23
  • 41
  • 4
    You may need to specify an absolute path to the executable, e.g. `exec(/usr/bin/wkhtmltopdf ...)`, if the sub-shell invoked by PHP ends up having a different PATH setting than your standard default shell. As well, if you're doing the exec from within a web-based script, then permissions are going to be a factor as well. – Marc B May 27 '11 at 14:16
  • Do you get any PHP error messages? What happens when you type that directly into the shell? – Alix Axel May 27 '11 at 14:37
  • 2
    check the apache error log, probably you will find the answer there ( like me ) – AgelessEssence Aug 30 '11 at 21:48

6 Answers6

10

had the same problem and i don't think anyone else should waste > 3 hours:

the solution is here: wkhtmltopdf error in apache log

you just have to install xvfp "to emulate a x-environment"

exec("xvfb-run -a wkhtmltopdf test.html output.pdf")
Community
  • 1
  • 1
Andrew Starlike
  • 379
  • 4
  • 14
4

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

Jimmy Sawczuk
  • 13,488
  • 7
  • 46
  • 60
  • interesting, just browsed it. HOWEVER, the command is the most handy for me because I want to make the conversion on the fly. And then, why the exec call is not executed? – Ginger Opariti May 27 '11 at 14:47
3

Here a PHP wrapper around wkhtmltopdf http://mikehaertl.github.com/phpwkhtmltopdf/ Very simple

nxplace
  • 421
  • 4
  • 5
2

Perhaps wkhtmltopdf is not in the PATH variable for www-data.

whereis wkhtmltopdf

will tell you where the binary is located; Binaries usually resides in /usr/bin/... on *nix machines. Then replace wkhtmltopdf with e.g. /usr/bin/wkhtmltopdf in your command like this.

/usr/bin/wkhtmltopdf entry.html output.pdf
joar
  • 15,077
  • 1
  • 29
  • 54
  • `type -P 'wkhtmltopdf'` is more reliable than `whereis`. – Alix Axel May 27 '11 at 14:28
  • I've also tried this:$input = getcwd() . "/entry.html"; $output = getcwd() . "/output.pdf"; exec ('/usr/bin/wkhtmltopdf $entry $output'); but same result. – Ginger Opariti May 27 '11 at 14:37
  • @user238831, then try `getcwd()` ([function.getcwd](http://www.php.net/manual/en/function.getcwd.php)) to verify that the script is executing in the correct directory. -- Look at what `getcwd()` returns. – joar May 27 '11 at 14:38
  • @Alix Axel -- Please elaborate, or provide sources. – joar May 27 '11 at 14:40
  • @user238831 -- Yes, did you look at what `getcwd()` said? Are you in the right directory? – joar May 27 '11 at 14:44
  • @jwandborg: http://unix.stackexchange.com/questions/10525/how-to-use-which-on-an-aliased-command/10529#10529 – Alix Axel May 27 '11 at 14:46
  • Also, try specifying the full path of the files. E.g., `exec("/usr/local/bin/wkhtml2pdf /tmp/whatever.html /var/www/output/output.pdf");` – Will Martin May 27 '11 at 14:46
  • @Will Martin - yes, see above. – Ginger Opariti May 27 '11 at 14:50
  • @Alix Axel -- That looks correct, as long as `whereis` and `which` are the same deal. – joar May 27 '11 at 14:50
  • @jwandborg - I've used getcwd() in the code above, all paths are secure – Ginger Opariti May 27 '11 at 14:51
  • @Alix Axel -- there's no other code than that ... I've put in comments all program to fix this one line problem. – Ginger Opariti May 27 '11 at 14:53
  • @user238831 -- Have you verified that both of the files exist at runtime? A tip is to print the output of the command, there's some details about it over at http://php.net/function.exec#refsect1-function.exec-returnvalues – joar May 27 '11 at 14:58
  • @jwandborg -- I've checked to have the input file. I don't get anything in output since the exec() statement is not executed – Ginger Opariti May 27 '11 at 15:03
  • 1
    @user238831 -- Of course the `exec` statement is executed, if you haven't commented it out. Use `$output = passthru('/usr/local/bin/wkhtml2pdf /tmp/foo.html /bar/foo.pdf'); echo $output;` so that you can see if the command line execution of `wkhtml2pdf` complains about anythin. – joar May 27 '11 at 20:34
  • @jwandborg - Many thanks for your time. It still doesn't work, I've done all the scenarios, replayed some of them already mentioned in other postings on SO, but no positive issue. It is a three line PHP that can be tested a proposal. For some reason "reason" doesn't play here. Very frustrating ... – Ginger Opariti May 30 '11 at 09:02
2

Just had this problem - simple solution in my case: I didn't realise that PHP was in Safe Mode. Switched off Safe Mode and it worked fine!

Phil
  • 21
  • 1
0

I was struggling with the same problem.

My solution on a Windows 2008 R2 server with PHP 5.4:

exec('C:\inetpub\wwwroot\mywebsite\subdir\wkhtmltopdf input.html output.pdf');

AND this was it (After > 5 hours searching the net including this) a new file called output.txt, renamed it to output.pdf and give the user 'everybody' rights to it.

These are my tryouts:

  exec(C:\inetpub\wwwroot\mywebsite\wkhtmltopdf );
  echo(exec(wkhtmltopdf.exe cache.html output.pdf));
  exec("xvfb-run -a wkhtmltopdf test.html output.pdf")
  $execute = "xvfb-run -a wkhtmltopdf cache.html output.pdf";
  $out = shell_exec("/path/to/wkhtmlto­pdf --version"); echo($out); 
  $out = passthru('/usr/local/bin/wkhtml2pdf

Hope these are usefull to others

jdkos
  • 7
  • 8