Skip to content
This repository

Improve wkhtmltopdf binary location finder. #100

Merged
merged 6 commits into from about 2 years ago

4 participants

Andrii Dmytrenko Ehtsham Abbas kevin lochner David Jones
Andrii Dmytrenko
Antti commented

Sometimes I'm getting this error:

undefined method `chomp' for nil:NilClass
wicked_pdf (0.7.5) lib/wicked_pdf.rb:21:in `initialize'

This commit should fix this, also it improves location finding by specifying possible paths.

kevin lochner

you should check the system path before searching your possible_locations array

You mean $PATH variable?
OK

I was just thinking you should try this after checking WickedPdf.config[:exe_path], but before checking your possible_locations:

exe_path ||= (defined?(Bundler) ? `bundle exec which wkhtmltopdf` : `which wkhtmltopdf`).chomp rescue ''

that way the executable is whatever is default for the user (if available), then you can add your extra options after that.

So code would be:

   exe_path = WickedPdf.config[:exe_path] unless WickedPdf.config.empty?
   exe_path ||= (defined?(Bundler) ? `bundle exec which wkhtmltopdf` : `which wkhtmltopdf`).chomp rescue nil
   exe_path ||= possible_locations.map{|l| File.expand_path("#{l}/#{EXE_PATH}") }.find{|location| File.exists? location}
   exe_path || ""

On some of our server configurations this throws non StandardError if we have Bundler defined, but no executable available (bin-stubbed bundler?)
So rescue '' will not work. Have to rescue everything, i.e: rescue Exception => e
Will try that tomorrow.

right - you can't use inline rescue to catch non StandardError - so you'll need an explicit begin/rescue/end block

you guys saved my life! :-D :+1:

I had this path problem and following your instructions now is working perfectly fine on HEROKU SERVER.
Thanks to @Antti and @klochner :-)

Andrii Dmytrenko
Antti commented

OK. Now we use which first and possible locations second.

David Jones unixmonkey merged commit 6829e52 into from
David Jones unixmonkey closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 6 unique commits by 1 author.

Mar 16, 2012
Andrii Dmytrenko Antti Improve wkhtmltopdf binary location finder. d384595
Andrii Dmytrenko Antti Fix typo. 7f7001f
Andrii Dmytrenko Antti Another Typo. Sorry. 6967cf6
Apr 19, 2012
Andrii Dmytrenko Antti Search for wkhtmltopdf in PATH first. cefb634
Apr 23, 2012
Andrii Dmytrenko Antti Fix wkhtmltopdf location search using `which`. cbb2d4d
Andrii Dmytrenko Antti Return nil here.
We must return nil(or false) so possible locations will be executed.
e88c6c9
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 16 additions and 6 deletions. Show diff stats Hide diff stats

  1. +16 6 lib/wicked_pdf.rb
22 lib/wicked_pdf.rb
@@ -12,16 +12,15 @@
12 12 require 'wicked_pdf_tempfile'
13 13
14 14 class WickedPdf
  15 + EXE_NAME = "wkhtmltopdf"
15 16 @@config = {}
16 17 cattr_accessor :config
17 18
18 19 def initialize(wkhtmltopdf_binary_path = nil)
19   - @exe_path = wkhtmltopdf_binary_path
20   - @exe_path ||= WickedPdf.config[:exe_path] unless WickedPdf.config.empty?
21   - @exe_path ||= (defined?(Bundler) ? `bundle exec which wkhtmltopdf` : `which wkhtmltopdf`).chomp
22   - raise "Location of wkhtmltopdf unknown" if @exe_path.empty?
23   - raise "Bad wkhtmltopdf's path" unless File.exists?(@exe_path)
24   - raise "Wkhtmltopdf is not executable" unless File.executable?(@exe_path)
  20 + @exe_path = wkhtmltopdf_binary_path || find_wkhtmltopdf_binary_path
  21 + raise "Location of #{EXE_NAME} unknown" if @exe_path.empty?
  22 + raise "Bad #{EXE_NAME}'s path" unless File.exists?(@exe_path)
  23 + raise "#{EXE_NAME} is not executable" unless File.executable?(@exe_path)
25 24 end
26 25
27 26 def pdf_from_string(string, options={})
@@ -182,4 +181,15 @@ def parse_others(options)
182 181 end
183 182 end
184 183
  184 + def find_wkhtmltopdf_binary_path
  185 + possible_locations = (ENV['PATH'].split(':')+%w[/usr/bin /usr/local/bin ~/bin]).uniq
  186 + exe_path ||= WickedPdf.config[:exe_path] unless WickedPdf.config.empty?
  187 + exe_path ||= begin
  188 + (defined?(Bundler) ? `bundle exec which wkhtmltopdf` : `which wkhtmltopdf`).chomp
  189 + rescue Exception => e
  190 + nil
  191 + end
  192 + exe_path ||= possible_locations.map{|l| File.expand_path("#{l}/#{EXE_NAME}") }.find{|location| File.exists? location}
  193 + exe_path || ''
  194 + end
185 195 end

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.