Execute wkhtmltopdf from PHP
Asked Answered
J

6

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.

Junkman answered 27/5, 2011 at 14:12 Comment(3)
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.Hunyadi
Do you get any PHP error messages? What happens when you type that directly into the shell?Stutman
check the apache error log, probably you will find the answer there ( like me )Rossierossing
A
4

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

Armstead answered 27/5, 2011 at 14:38 Comment(1)
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?Junkman
B
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")
Bui answered 7/10, 2012 at 0:43 Comment(1)
Still fails saying "radeon: Invalid PCI ID."Moly
A
4

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

Armstead answered 27/5, 2011 at 14:38 Comment(1)
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?Junkman
S
3

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

Shavon answered 14/11, 2012 at 0:27 Comment(0)
P
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
Pentecostal answered 27/5, 2011 at 14:19 Comment(15)
type -P 'wkhtmltopdf' is more reliable than whereis.Stutman
I've also tried this:$input = getcwd() . "/entry.html"; $output = getcwd() . "/output.pdf"; exec ('/usr/bin/wkhtmltopdf $entry $output'); but same result.Junkman
@user238831, then try getcwd() (function.getcwd) to verify that the script is executing in the correct directory. -- Look at what getcwd() returns.Pentecostal
@Alix Axel -- Please elaborate, or provide sources.Pentecostal
@user238831 -- Yes, did you look at what getcwd() said? Are you in the right directory?Pentecostal
@jwandborg: unix.stackexchange.com/questions/10525/…Stutman
Also, try specifying the full path of the files. E.g., exec("/usr/local/bin/wkhtml2pdf /tmp/whatever.html /var/www/output/output.pdf");Wellfounded
@Will Martin - yes, see above.Junkman
@Alix Axel -- That looks correct, as long as whereis and which are the same deal.Pentecostal
@jwandborg - I've used getcwd() in the code above, all paths are secureJunkman
@Alix Axel -- there's no other code than that ... I've put in comments all program to fix this one line problem.Junkman
@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 php.net/function.exec#refsect1-function.exec-returnvaluesPentecostal
@jwandborg -- I've checked to have the input file. I don't get anything in output since the exec() statement is not executedJunkman
@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.Pentecostal
@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 ...Junkman
C
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!

Cerallua answered 23/7, 2011 at 11:28 Comment(0)
G
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

Godmother answered 4/1, 2013 at 2:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.