lib/wicked_pdf.rb in wicked_pdf-0.7.9 vs lib/wicked_pdf.rb in wicked_pdf-0.8.0

- old
+ new

@@ -4,26 +4,30 @@ require 'logger' require 'digest/md5' require 'rbconfig' require RbConfig::CONFIG['target_os'] == 'mingw32' && !(RUBY_VERSION =~ /1.9/) ? 'win32/open3' : 'open3' require 'active_support/core_ext/class/attribute_accessors' -require 'active_support/core_ext/object/blank' +begin + require 'active_support/core_ext/object/blank' +rescue LoadError + require 'active_support/core_ext/blank' +end + require 'wicked_pdf_railtie' require 'wicked_pdf_tempfile' class WickedPdf + EXE_NAME = "wkhtmltopdf" @@config = {} cattr_accessor :config def initialize(wkhtmltopdf_binary_path = nil) - @exe_path = wkhtmltopdf_binary_path - @exe_path ||= WickedPdf.config[:exe_path] unless WickedPdf.config.empty? - @exe_path ||= (defined?(Bundler) ? `bundle exec which wkhtmltopdf` : `which wkhtmltopdf`).chomp - raise "Location of wkhtmltopdf unknown" if @exe_path.empty? - raise "Bad wkhtmltopdf's path" unless File.exists?(@exe_path) - raise "Wkhtmltopdf is not executable" unless File.executable?(@exe_path) + @exe_path = wkhtmltopdf_binary_path || find_wkhtmltopdf_binary_path + raise "Location of #{EXE_NAME} unknown" if @exe_path.empty? + raise "Bad #{EXE_NAME}'s path" unless File.exists?(@exe_path) + raise "#{EXE_NAME} is not executable" unless File.executable?(@exe_path) end def pdf_from_string(string, options={}) command = "\"#{@exe_path}\" #{'-q ' unless on_windows?}#{parse_options(options)} - - " # -q for no errors on stdout print_command(command) if in_development_mode? @@ -42,12 +46,12 @@ end private def in_development_mode? - (defined?(Rails) && Rails.env == 'development') || - (defined?(RAILS_ENV) && RAILS_ENV == 'development') + return Rails.env == 'development' if defined?(Rails) + RAILS_ENV == 'development' if defined?(RAILS_ENV) end def on_windows? RbConfig::CONFIG['target_os'] == 'mingw32' end @@ -194,6 +198,17 @@ :use_xserver, :no_background], "", :boolean) end end + def find_wkhtmltopdf_binary_path + possible_locations = (ENV['PATH'].split(':')+%w[/usr/bin /usr/local/bin ~/bin]).uniq + exe_path ||= WickedPdf.config[:exe_path] unless WickedPdf.config.empty? + exe_path ||= begin + (defined?(Bundler) ? `bundle exec which wkhtmltopdf` : `which wkhtmltopdf`).chomp + rescue Exception => e + nil + end + exe_path ||= possible_locations.map{|l| File.expand_path("#{l}/#{EXE_NAME}") }.find{|location| File.exists? location} + exe_path || '' + end end