Class: PDFKit::WkHTMLtoPDF

Inherits:
Object
  • Object
show all
Defined in:
lib/pdfkit/wkhtmltopdf.rb

Constant Summary collapse

REPEATABLE_OPTIONS =
%w[--allow --bypass-proxy-for --cookie --custom-header --post --post-file --run-script --replace].freeze
SPECIAL_OPTIONS =
%w[cover toc].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ WkHTMLtoPDF

Returns a new instance of WkHTMLtoPDF.



11
12
13
# File 'lib/pdfkit/wkhtmltopdf.rb', line 11

def initialize(options)
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/pdfkit/wkhtmltopdf.rb', line 5

def options
  @options
end

Instance Method Details

#error_handling?Boolean

Returns:

  • (Boolean)


39
40
41
42
43
44
# File 'lib/pdfkit/wkhtmltopdf.rb', line 39

def error_handling?
  @options.key?('--ignore-load-errors') ||
    # wkhtmltopdf v0.10.0 beta4 replaces ignore-load-errors with load-error-handling
    # https://code.google.com/p/wkhtmltopdf/issues/detail?id=55
    %w(skip ignore).include?(@options['--load-error-handling'])
end

#normalize_optionsObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/pdfkit/wkhtmltopdf.rb', line 15

def normalize_options
  # TODO(cdwort,sigmavirus24): Make this method idempotent in a future release so it can be called repeatedly
  normalized_options = {}
  
  @options.each do |key, value|
    next if !value
  
    # The actual option for wkhtmltopdf
    normalized_key = normalize_arg key
    normalized_key = "--#{normalized_key}" unless SPECIAL_OPTIONS.include?(normalized_key)
  
    # If the option is repeatable, attempt to normalize all values
    if REPEATABLE_OPTIONS.include? normalized_key
      normalize_repeatable_value(normalized_key, value) do |normalized_unique_key, normalized_value|
        normalized_options[normalized_unique_key] = normalized_value
      end
    else # Otherwise, just normalize it like usual
      normalized_options[normalized_key] = normalize_value(value)
    end
  end
  
  @options = normalized_options
end

#options_for_commandObject



46
47
48
# File 'lib/pdfkit/wkhtmltopdf.rb', line 46

def options_for_command
  @options.to_a.flatten.compact
end