8

There have been a few topics similar to mine, but they did not help me. Maybe something new will come up.

Problem: I can't execute wkhtmltopdf form PHP. My basic code is: exec('wkhtmltopdf http://somesite.com /home/user/file.pdf');

Now a few things I already checked:

  • the same command works when executed from console
  • safe_mode is disabled - I can execute commands in PHP, for example exec('ls'); works fine
  • path to wkhtmltopdf can be found and I can run the program itself, for example exec('wkhtmltopdf -V'); works fine and outputs versioning info
  • the output path is writable, it has 777 permissions and exec('wkhtmltopdf -V > /home/user/file.pdf'); works fine, creates the file and puts versioning info inside
  • executing from PHP has internet access, for exemple exec('wget -O /home/user/file.pdf http://somesite.com') works fine, retrieves the site source and puts it into the file
  • replacing exec with passthru, shell_exec etc. makes no difference

Concluding: I can execute wkhtmltopdf, I can access internet, I can write the specific file, but a correct conversion command fails and returns status 1.

I use the latest Ubuntu, PHP Version is 5.3.10-1ubuntu3, wkhtmltopdf version is 0.9.9. I tried it also on some other machine with the same retults.

Any (just ANY) ideas will be greatly appreciated!

Krzychu
  • 241
  • 2
  • 4
  • 12
  • Problem solved. It was a common issue which happens when you install wkhtmltopdf via apt-get. I had to remove it and download directly from code.google.com/p/wkhtmltopdf. Now it works. – Krzychu May 21 '12 at 13:22

1 Answers1

16

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

dakdad
  • 2,947
  • 2
  • 22
  • 21
  • 1
    Man, you saved my life! Your idea worked and showed me an error "cannot connect to X server". Then it was easy to find that it is a common problem when you install wkhtmltopdf via apt-get. I had to remove it and download directly from http://code.google.com/p/wkhtmltopdf/. Now it works :) Thanks so much! – Krzychu May 21 '12 at 13:18