1

I'm getting the following error while running Snappy, a PHP binding for WKHTMLTOPDF:

Fatal error: Uncaught exception 'RuntimeException' with message 'The file '/Users/username/test.pdf' was not created (command: /usr/bin/wkhtmltopdf --lowquality '/var/folders/--/--ze9OC9GTSBW3tCl6UCR++++TQ/-Tmp-/knp_snappy4f761d35744a96.74626529.html' '/Users/username/test.pdf').' in /Applications/MAMP/htdocs/includes/wkhtmltopdf/Knp/Snappy/AbstractGenerator.php:261 Stack trace: #0 /Applications/MAMP/htdocs/includes/wkhtmltopdf/Knp/Snappy/AbstractGenerator.php(117): Knp\Snappy\AbstractGenerator->checkOutput('/Users/username...', '/usr/bin/wkhtml...') #1 /Applications/MAMP/htdocs/includes/wkhtmltopdf/Knp/Snappy/AbstractGenerator.php(127): Knp\Snappy\AbstractGenerator->generate('/var/folders/--...', '/Users/username...', Array, false) #2 /Applications/MAMP/htdocs/test.php(14): Knp\Snappy\AbstractGenerator->generateFromHtml('<h1>Test</h1>', '/Users/username...') #3 {main} thrown in /Applications/MAMP/htdocs/includes/wkhtmltopdf/Knp/Snappy/AbstractGenerator.php on line 261

However, if I run what they execute via command line, it works just fine:

/usr/bin/wkhtmltopdf --lowquality '/var/folders/--/--ze9OC9GTSBW3tCl6UCR++++TQ/-Tmp-/knp_snappy4f761d35744a96.74626529.html' '/Users/username/test.pdf

Since that works just find, one would think I could execute it via the exec() function or the shell_exec() function, both of which return nothing at all (no error or anything). Executing 'whoami' does return my username though.

Safe mode is off and the permissions on the wkhtmltopdf file are set to 777.

I know there are a couple other threads similar to this but there were either work arounds from the initial asker that made little sense or no solution at all. Can anyone provide any insight here?

Michal
  • 3,262
  • 4
  • 30
  • 50
Chords
  • 6,720
  • 2
  • 39
  • 61
  • any particular reason why you're using snappy? I found it to be little useful... take a look at http://stackoverflow.com/questions/9910975/pdf2html-in-php-convert-untilities-scripts-examples-demos/9924154#9924154 – Michal Mar 30 '12 at 21:10
  • Nope, no particular reason, just seems to be a lot of support behind it. I'm reading through your post now and, embarrassingly, have a question already. I'm running MAMP so how can I run the apt-get command? – Chords Mar 30 '12 at 21:26
  • :) that's beyond me, but `apt-get` is a package manager for linux (makes it really easy to install stuff on the server), so you could look at this http://superuser.com/questions/92872/apple-mac-os-x-is-there-a-package-manager-like-linux – Michal Mar 30 '12 at 21:33
  • 1
    I tried for hours to get snappy to run and gave it up in the end. I received the same error you describe and even some more :) looking back now it was not worth the time lost at all... – Michal Mar 30 '12 at 21:35
  • Thanks for you help Michal; Snappy (or any other bindings) is definitely not the fastest, best or most clear way to get this up and running. – Chords Apr 10 '12 at 17:31

1 Answers1

3

Checkout this answer's source at: http://oneqonea.blogspot.com/2012/04/why-does-wkhtmltopdf-work-via-terminal.html

Within MAMP's /Applications/MAMP/Library/bin/envvars file you'll notice the following two lines:

DYLD_LIBRARY_PATH="/Applications/MAMP/Library/lib:$DYLD_LIBRARY_PATH"
export DYLD_LIBRARY_PATH

Comment both of them out as demonstrated below (note the "#" prefix on each line):

#DYLD_LIBRARY_PATH="/Applications/MAMP/Library/lib:$DYLD_LIBRARY_PATH"
#export DYLD_LIBRARY_PATH

Lastly, within the same file, add the following command to make sure the $PATH environment variable inherited by PHP from Apache includes the directory that contains your wkhtmltopdf executable. Your command will look something like:

export PATH=/parent/path/of/wkhtmltopdf/executable:$PATH
John Erck
  • 9,478
  • 8
  • 61
  • 71
  • Thanks, John. I had already done this, but I managed to get it working this time around while tinkering, so I'll mark this as correct since it was a step. Thanks for getting me moving on this again! – Chords Apr 10 '12 at 17:29