No wkhtmltopdf executable found #123

Closed
giacomopiva opened this Issue Dec 27, 2011 · 20 comments

9 participants

@giacomopiva

I'm using rails 3.0.9 and ruby 1.8.7 and I'm getting this error:

No wkhtmltopdf executable found at

Please install wkhtmltopdf - https://github.com/jdpace/PDFKit/wiki/Installing-WKHTMLTOPDF

The executable is installed and accessible (in the PATH) and it have 755 permissions.

@abelmartin

I'm also seeing the same problem on our prod server (Ruby 1.8.7p352, Rails 3.0.10, PDFKit 0.5.2, wkhtmltopdf 0.9.6). I didn't used to have this issue in prod with our Rails 2.3.2 app with wkhtmltopdf 0.9.6, but we recently upgraded.

What's odd is that if I run a simple HTML to PDF conversion from the rails console (in production environment), it works just fine.

Could this error message be masking another error?

-a

@stuart

Also seeing this issue on Rails 2.3.10, PDFKit 0.5.2 and wkhtmltopdf 0.9.6.
PDFKit.configuration.wkhtmltopdf is not set in the running application. It is set correctly when I use PDFKit in the console.

Found the problem was with line 20 of configuration.rb

    @wkhtmltopdf ||= (defined?(Bundler::GemfileError) ? `bundle exec which wkhtmltopdf` : `which wkhtmltopdf`).chomp

This was giving a non existent path with bundle exec. 
The workaround was to set the path in an initializer like so:

PDFKit.configure do |config|
  config.wkhtmltopdf = `which wkhtmltopdf`
end
@abelmartin

Ha!

I was just about to post a similar solution. Mine's more static, but ultimately the same thing:

if Rails.env.production?

  PDFKit.configure do |config|
    config.wkhtmltopdf = '/usr/local/bin/wkhtmltopdf'
  end

end

...in my config/initializers/pdfkit.rb :) I prefer your solution @stuart since it works in all environments without a messy conditional.

-a

@giacomopiva

Stuart hit the problem.
In my case the problem is the production environment has many version of bundle installed, and the bundle for ruby 1.8.7 the executable name is bundle187, so I changed the line with:

@wkhtmltopdf ||= (defined?(Bundler::GemfileError) ?bundle187 exec which wkhtmltopdf:which wkhtmltopdf`).chomp

Now it works in my case.

@vcabana

Good day to all!

I have tried the solutions you provided above and still I'm experiencing the same issue. May you please help me fix this issue(No wkhtmltopdf executable found)?

FYI, I'm using windows pc and have successfully installed the wkhtmltopdf.

Thanks,

Vic

@tdm00

I'm hitting this same problem. I have a user setup on my Linux system named deploy, with RVM and Ruby 1.9.3 install there. I have the gem 'wkhtmltopdf-binary' and gem 'pdfkit' in my Gemfile, both are installed on the system. When I attempt to render the HTML file to a PDF I'm told:
```PDFKit::NoExecutableError (No wkhtmltopdf executable found at

Please install wkhtmltopdf - https://github.com/jdpace/PDFKit/wiki/Installing-WKHTMLTOPDF):
pdfkit (0.5.2) lib/pdfkit/pdfkit.rb:29:in initialize'
pdfkit (0.5.2) lib/pdfkit/middleware.rb:21:in
new'
pdfkit (0.5.2) lib/pdfkit/middleware.rb:21:in `call'

If I type `which wkhtmltopdf` then I'm told:

/home/deploy/.rvm/gems/ruby-1.9.3-p194/bin/wkhtmltopdf

@vcabana

Good day, Troy!

Please see below for the setup process I have implemented on my windows PC to fix the above issue.

PDF KIT
1. Install wkhtmltopdf.
a. For Windows
i) Download wkhtmltopdf-0.9.9-installer.exe .
ii) Install it to this directory C:\wkhtmltopdf to avoid permission issue of the app when integrating it to PDF KIT.
b. For Mac

   curl -O http://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-0.9.9-OS-X.i368
  mv wkhtmltopdf-0.9.9-OS-X.i368 /usr/local/bin/wkhtmltopdf
  chmod +x /usr/local/bin/wkhtmltopdf
c.  For Mac with Homebrew

 brew install WKHTMLTOPDF

d.  Linux(Arch Linux)

 # Install the wkhtmltopdf package from community
 sudo pacman -Sy wkhtmltopdf

e.  Linux(Ubuntu)

 # Install the wkhtmltopdf package
 sudo apt-get install wkhtmltopdf
 or
 # first, installing dependencies
 sudo aptitude install openssl build-essential xorg libssl-dev

 # for 64bits OS
wget http://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-0.9.9-static-amd64.tar.bz2 
tar xvjf wkhtmltopdf-0.9.9-static-amd64.tar.bz2
mv wkhtmltopdf-amd64 /usr/local/bin/wkhtmltopdf
chmod +x /usr/local/bin/wkhtmltopdf

# for 32bits OS
wget http://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-0.9.9-static-i386.tar.bz2 
tar xvjf wkhtmltopdf-0.9.9-static-i386.tar.bz2
mv wkhtmltopdf-i386 /usr/local/bin/wkhtmltopdf
chmod +x /usr/local/bin/wkhtmltopdf

 f. Linux (Gentoo on Rackspace Cloud)
 # search for the following first using emerge --search <files here> I've just put the ones i needed.
 # Note that we may find an easier way later, but wkhtmltopdf responded each time needing these pkgs.
   sudo emerge libXext app-admin/eselect-fontconfig x11-libs/libXrender

 # for 64bits OS
 wget http://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-0.9.9-static-amd64.tar.bz2 
 tar xvjf wkhtmltopdf-0.9.9-static-amd64.tar.bz2
 mv wkhtmltopdf-amd64 /usr/local/bin/wkhtmltopdf
 chmod +x /usr/local/bin/wkhtmltopdf
  1. Write gem ‘pdfkit’ in the Gemfile. Also, the gem ‘wkhtmltopdf-binary’ for mac + linux i386 OS.
  2. Run bundle install
  3. Write the following lines in the application.rb file: # Middle ware for PDF Kit config.middleware.use 'PDFKit::Middleware'
  4. Create a file named pdfkit.rb in the config/initializers and write the following lines:

PDF Kit

PDFKit.configure do |config|
config.wkhtmltopdf = 'C:/wkhtmltopdf/wkhtmltopdf.exe'
config.default_options = {
:page_size => 'Legal',
:print_media_type => true

config.root_url = "http://localhost" # Use only if your external hostname is unavailable on the server.

}
End

  1. Create a file named pdfkit_asset.rb in the config/initializers and write the following lines:

ActionController::Base.asset_host = Proc.new { |source, request|
if request.env["REQUEST_PATH"].include? ".pdf"
"file://#{Rails.root.join('public')}"
else
"#{request.protocol}#{request.host_with_port}"
end
}

  1. In application.html file, add :media property. See example below. <%= stylesheet_link_tag 'application', :media => "all" %>
  2. In your page, just append .pdf to test if it is working or specify a link to convert a page to PDF. See example below.

References: https://github.com/pdfkit/PDFKit
https://github.com/pdfkit/pdfkit/wiki/Installing-WKHTMLTOPDF
http://railscasts.com/episodes/220-pdfkit
http://code.google.com/p/wkhtmltopdf/

I hope the above process will fix the issue you have on your Linux system.

Regards,

Vic Abana

@tdm00

@viclnp that helped me get a little further. I've removed the wkhtmltopdf-executable gem from my Gemfile, then re-deployed to my server via Capistrano. However now I'm getting this message when trying to generate a PDF:

RuntimeError (command failed: "/home/deploy/apps/myapp/shared/bundle/ruby/1.9.1/bin/wkhtmltopdf" "--page-size" "Letter" "--margin-top" "0.75in" "--margin-right" "0.75in" "--margin-bottom" "0.75in" "--margin-left" "0.75in" "--encoding" "UTF-8" "--print-media-type" "--quiet" "-" "-"):

Not sure why that's still show up there, I thought removing it from my Gemfile would remove it. If I do
/home/deploy/apps/myapp/shared/bundle/ruby/1.9.1/bin/wkhtmltopdf --version it reports the version number correctly.

@vcabana

@talltroym - On your local PC, please generate pdf using your terminal? If the same issue occurs, then can you move the directory of your wktmltopdf to your local drive or to your desktop then change the directory of the config.wkhtmltopdf you defined in the pdfkit.rb from config/initializers.

Hope that helps and will fix the issue.

Vic Abana

@tdm00

@viclnp - thanks for the replies. I setup a new virtual server and I was able to configure this to work. I think the problem is on my production server in the Gemfile I had the "wkhtmltopdf-executable" in the Gemfile, so it installed there. Now I've removed it from the Gemfile, but that didn't remove the gem from the system (I'm using Capistrano). I'll either figure out how to remove that one gem in that shared directory, or wipe out the app and re-deploy it without that gem, but having the wkhtmltopdf installed on the server like your directions show. This is what I did on the virtual server to get it to work.

@vcabana

@talltroym - You are welcome! :)

@Blakey

Hi,

I am having real trouble getting PDFkit to work with ruby on Windows 7 system.

I am trying to follow the steps above but have no idea what to do at step 4:

 "Create a file named pdfkit.rb in the config/initializers and write the following lines:

PDF Kit

PDFKit.configure do |config|
config.wkhtmltopdf = 'C:/wkhtmltopdf/wkhtmltopdf.exe'
config.default_options = {
:page_size => 'Legal',
:print_media_type => true
config.root_url = "http://localhost" # Use only if your external hostname is unavailable on the server.

}
End"

Where is config/initializers ?? i cannot find it anywhere.

Appreciate any help.

Thanks

@tdm00
@Blakey

OK that explains why i cannot find it. I am not running rails (Not ready for building a web app yet - just trying to speed my life up a bit with a nifty bit of programming). Is PDFKit not going to work without rails? Or do i just need to update the config elsewhere? Sorry - pretty new to all of this!

@sodabrew

You don't need to be in Rails. You just need to run the "PDFKit.configure do |config| ... " block from your code prior to using PDFkit.

@Blakey

Thanks guys. I have tried running from my code with no success. I'm going to start from scratch again and will hopefully see what has gone wrong. Will report back soon :)

Update: I deleted, re-installed everything and used above code and it wokrs perfectly now.

Thanks sodabrew and talltroym

@mcljot

Hi folks, sorry to resurrect an old thread. I have the same issue that Blakey had, but which file do I paste the PDFKit.configure do stuff into?

Cheers!

@vcabana

Just read this @mcljot. Is this resolved? Thanks.

@mcljot
@vcabana

Great to hear @mcljot! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment