Monday, April 9, 2012

Why does wkhtmltopdf work via Terminal and fail via PHP's shell_exec() when using MAMP?

Question: Why does the following command line command:

wkhtmltopdf /path/to/html/input/file.html /path/to/pdf/output/file.pdf

work as expected when executed via Mac OSX Terminal but fail when executed via PHP's shell_exec() function via MAMP?

Answer: You need to comment out two lines of code within one of MAMP's configuration files.

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

Looking for a cloud-based catalog and CRM platform? Checkout http://rocware.com/

11 comments:

  1. Thanks a lot. You are a savior. I had this exact issue on my MAC which has MAMP and after a lot of searching and debugging I found your page. I have this same issue on my production machine which is having redhat and a normal Apache setup. No MAMP. Any insights as to how I can make it work there?

    ReplyDelete
    Replies
    1. I'm glad my post helped you out. I didn't have any issues when deploying to my production machines running Debian Squeeze. If you figure out the solution though you should consider adding it as a comment to this post for other users experiencing the same issue you're facing. Thanks.

      Delete
  2. I was able to get it to work. It was a permissions issue. The SELinux firewall was not allowing apache to spawn a new process for the wkhtmltopdf binary. All I had to do was set the "httpd_execmem" boolean in SELinux to ON. You can do this by executing the command "setsebool httpd_execmem on" on your terminal.

    ReplyDelete
  3. man i could kiss you for this =)

    ReplyDelete
  4. Well researched article and I appreciate this. The blog is subscribed and will see new topics soon.
    Data Science training in Chennai
    Data science online training

    ReplyDelete
  5. Thanks for your informative article, Your post helped me to understand the future and career prospects & Keep on updating your blog with such awesome article.
    Best Devops online Training
    Online DevOps Certification Course - Gangboard

    ReplyDelete
  6. I was taking a gander at some of your posts on this site and I consider this site is truly informational! Keep setting up digital marketing platforms

    ReplyDelete

About Me

My photo
I code. I figured I should start a blog that keeps track of the many questions and answers that are asked and answered along the way. The name of my blog is "One Q, One A". The name describes the format. When searching for an answer to a problem, I typically have to visit more than one site to get enough information to solve the issue at hand. I always end up on stackoverflow.com, quora.com, random blogs, etc before the answer is obtained. In my blog, each post will consist of one question and one answer. All the noise encountered along the way will be omitted.