lib/wicked_pdf.rb in wicked_pdf-1.0.4 vs lib/wicked_pdf.rb in wicked_pdf-1.0.5
- old
+ new
@@ -2,10 +2,11 @@
# http://code.google.com/p/wkhtmltopdf/
require 'logger'
require 'digest/md5'
require 'rbconfig'
+require 'shellwords'
if (RbConfig::CONFIG['target_os'] =~ /mswin|mingw/) && (RUBY_VERSION < '1.9')
require 'win32/open3'
else
require 'open3'
@@ -29,21 +30,22 @@
require 'wicked_pdf/middleware'
class WickedPdf
DEFAULT_BINARY_VERSION = Gem::Version.new('0.9.9')
BINARY_VERSION_WITHOUT_DASHES = Gem::Version.new('0.12.0')
- EXE_NAME = 'wkhtmltopdf'
+ EXE_NAME = 'wkhtmltopdf'.freeze
@@config = {}
cattr_accessor :config
+ attr_accessor :binary_version
def initialize(wkhtmltopdf_binary_path = nil)
@exe_path = wkhtmltopdf_binary_path || find_wkhtmltopdf_binary_path
- fail "Location of #{EXE_NAME} unknown" if @exe_path.empty?
- fail "Bad #{EXE_NAME}'s path: #{@exe_path}" unless File.exist?(@exe_path)
- fail "#{EXE_NAME} is not executable" unless File.executable?(@exe_path)
+ raise "Location of #{EXE_NAME} unknown" if @exe_path.empty?
+ raise "Bad #{EXE_NAME}'s path: #{@exe_path}" unless File.exist?(@exe_path)
+ raise "#{EXE_NAME} is not executable" unless File.executable?(@exe_path)
- retreive_binary_version
+ retrieve_binary_version
end
def pdf_from_html_file(filepath, options = {})
pdf_from_url("file:///#{filepath}", options)
end
@@ -84,11 +86,11 @@
return generated_pdf_file
end
generated_pdf_file.rewind
generated_pdf_file.binmode
pdf = generated_pdf_file.read
- fail "PDF could not be generated!\n Command Error: #{err}" if pdf && pdf.rstrip.length == 0
+ fail "PDF could not be generated!\n Command Error: #{err}" if pdf && pdf.rstrip.empty?
pdf
rescue => e
raise "Failed to execute:\n#{command}\nError: #{e}"
ensure
generated_pdf_file.close! if generated_pdf_file && !return_file
@@ -99,26 +101,23 @@
def in_development_mode?
return Rails.env == 'development' if defined?(Rails)
RAILS_ENV == 'development' if defined?(RAILS_ENV)
end
- def get_binary_version
- @binary_version
- end
-
def on_windows?
RbConfig::CONFIG['target_os'] =~ /mswin|mingw/
end
def print_command(cmd)
p '*' * 15 + cmd + '*' * 15
end
- def retreive_binary_version
+ def retrieve_binary_version
_stdin, stdout, _stderr = Open3.popen3(@exe_path + ' -V')
@binary_version = parse_version(stdout.gets(nil))
rescue StandardError
+ DEFAULT_BINARY_VERSION
end
def parse_version(version_info)
match_data = /wkhtmltopdf\s*(\d*\.\d*\.\d*\w*)/.match(version_info)
if match_data && (2 == match_data.length)
@@ -161,21 +160,21 @@
def make_option(name, value, type = :string)
if value.is_a?(Array)
return value.collect { |v| make_option(name, v, type) }
end
if type == :name_value
- parts = value.to_s.split(' ')
+ parts = value.to_s.split(' ').map { |part| part.shellescape }
["--#{name.tr('_', '-')}", *parts]
elsif type == :boolean
["--#{name.tr('_', '-')}"]
else
- ["--#{name.tr('_', '-')}", value.to_s]
+ ["--#{name.tr('_', '-')}", value.to_s.shellescape]
end
end
def valid_option(name)
- if get_binary_version < BINARY_VERSION_WITHOUT_DASHES
+ if binary_version < BINARY_VERSION_WITHOUT_DASHES
"--#{name}"
else
name
end
end
@@ -196,12 +195,12 @@
def parse_header_footer(options)
r = []
[:header, :footer].collect do |hf|
next if options[hf].blank?
opt_hf = options[hf]
- r += make_options(opt_hf, [:center, :font_name, :left, :right], "#{hf}")
- r += make_options(opt_hf, [:font_size, :spacing], "#{hf}", :numeric)
- r += make_options(opt_hf, [:line], "#{hf}", :boolean)
+ r += make_options(opt_hf, [:center, :font_name, :left, :right], hf.to_s)
+ r += make_options(opt_hf, [:font_size, :spacing], hf.to_s, :numeric)
+ r += make_options(opt_hf, [:line], hf.to_s, :boolean)
if options[hf] && options[hf][:content]
@hf_tempfiles = [] unless defined?(@hf_tempfiles)
@hf_tempfiles.push(tf = WickedPdfTempfile.new("wicked_#{hf}_pdf.html"))
tf.write options[hf][:content]
tf.flush