lib/wicked_pdf.rb in wicked_pdf-0.7.2 vs lib/wicked_pdf.rb in wicked_pdf-0.7.3
- old
+ new
@@ -1,12 +1,14 @@
# wkhtml2pdf Ruby interface
# http://code.google.com/p/wkhtmltopdf/
require 'logger'
require 'digest/md5'
-require 'open3'
+require 'rbconfig'
+require Config::CONFIG['target_os'] == 'mingw32' ? 'win32/open3' : 'open3'
require 'active_support/core_ext/class/attribute_accessors'
+require 'active_support/core_ext/object/blank'
require 'wicked_pdf_railtie'
require 'wicked_pdf_tempfile'
class WickedPdf
@@ -14,19 +16,19 @@
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 ||= `which wkhtmltopdf`.chomp
+ @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)
end
def pdf_from_string(string, options={})
- command = "#{@exe_path} #{parse_options(options)} -q - - " # -q for no errors on stdout
- p "*"*15 + command + "*"*15 unless defined?(Rails) and Rails.env != 'development'
+ command = "\"#{@exe_path}\" #{parse_options(options)} #{'-q ' unless on_windows?}- - " # -q for no errors on stdout
+ print_command(command) if in_development_mode?
pdf, err = Open3.popen3(command) do |stdin, stdout, stderr|
stdin.binmode
stdout.binmode
stderr.binmode
stdin.write(string)
@@ -39,10 +41,23 @@
raise "Failed to execute:\n#{command}\nError: #{e}"
end
private
+ def in_development_mode?
+ (defined?(Rails) && Rails.env == 'development') ||
+ (RAILS_ENV && RAILS_ENV == 'development')
+ end
+
+ def on_windows?
+ Config::CONFIG['target_os'] == 'mingw32'
+ end
+
+ def print_command(cmd)
+ p "*"*15 + cmd + "*"*15
+ end
+
def parse_options(options)
[
parse_header_footer(:header => options.delete(:header),
:footer => options.delete(:footer),
:layout => options[:layout]),
@@ -62,14 +77,18 @@
""
end
end
def make_option(name, value, type=:string)
+ if value.respond_to? :each
+ return value.collect { |v| make_option(name, v, type) }.join('')
+ end
"--#{name.gsub('_', '-')} " + case type
when :boolean then ""
when :numeric then value.to_s
- else "'#{value}'"
+ when :name_value then value.to_s
+ else "\"#{value}\""
end + " "
end
def make_options(options, names, prefix="", type=:string)
names.collect {|o| make_option("#{prefix.blank? ? "" : prefix + "-"}#{o.to_s}", options[o], type) unless options[o].blank?}.join
@@ -90,12 +109,13 @@
end unless options.blank?
r
end
def parse_toc(options)
+ r = '--toc ' unless options.nil?
unless options.blank?
- r = make_options(options, [ :font_name, :header_text], "toc")
+ r += make_options(options, [ :font_name, :header_text], "toc")
r +=make_options(options, [ :depth,
:header_fs,
:l1_font_size,
:l2_font_size,
:l3_font_size,
@@ -112,10 +132,11 @@
:l7_indentation], "toc", :numeric)
r +=make_options(options, [ :no_dots,
:disable_links,
:disable_back_links], "toc", :boolean)
end
+ return r
end
def parse_outline(options)
unless options.blank?
r = make_options(options, [:outline], "", :boolean)
@@ -138,9 +159,11 @@
:password,
:cover,
:dpi,
:encoding,
:user_style_sheet])
+ r +=make_options(options, [ :cookie,
+ :post], "", :name_value)
r +=make_options(options, [ :redirect_delay,
:zoom,
:page_offset], "", :numeric)
r +=make_options(options, [ :book,
:default_header,