Generating PDF from PHP using Exec() or Bindings-Collection of common programming errors
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('
Test
', '/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?
-
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
Originally posted 2013-11-27 05:07:54.