Class: PDFKit::WkHTMLtoPDF

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

Constant Summary collapse

REPEATABLE_OPTIONS =
%w[--allow --cookie --custom-header --post --post-file --run-script]
SPECIAL_OPTIONS =
%w[cover toc]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ WkHTMLtoPDF

Returns a new instance of WkHTMLtoPDF.



9
10
11
# File 'lib/pdfkit/wkhtmltopdf.rb', line 9

def initialize(options)
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/pdfkit/wkhtmltopdf.rb', line 3

def options
  @options
end

Instance Method Details

#error_handling?Boolean

Returns:

  • (Boolean)


37
38
39
40
41
42
# File 'lib/pdfkit/wkhtmltopdf.rb', line 37

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



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

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



44
45
46
# File 'lib/pdfkit/wkhtmltopdf.rb', line 44

def options_for_command
  @options.to_a.flatten.compact
end