Class: PDFKit

Inherits:
Object
  • Object
show all
Defined in:
lib/pdfkit/os.rb,
lib/pdfkit/pdfkit.rb,
lib/pdfkit/source.rb,
lib/pdfkit/version.rb,
lib/pdfkit/middleware.rb,
lib/pdfkit/wkhtmltopdf.rb,
lib/pdfkit/configuration.rb,
lib/pdfkit/html_preprocessor.rb

Defined Under Namespace

Modules: HTMLPreprocessor, OS Classes: Configuration, Error, ImproperSourceError, ImproperWkhtmltopdfExitStatus, Middleware, NoExecutableError, Source, WkHTMLtoPDF

Constant Summary collapse

VERSION =
'0.8.4.3.2'

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url_file_or_html, options = {}) ⇒ PDFKit

Returns a new instance of PDFKit.

Raises:


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/pdfkit/pdfkit.rb', line 30

def initialize(url_file_or_html, options = {})
  @source = Source.new(url_file_or_html)

  @stylesheets = []

  options = PDFKit.configuration.default_options.merge(options)
  options.delete(:quiet) if PDFKit.configuration.verbose?
  options.merge! find_options_in_meta(url_file_or_html) unless source.url?
  @root_url = options.delete(:root_url)
  @protocol = options.delete(:protocol)
  @renderer = WkHTMLtoPDF.new options
  @renderer.normalize_options

  raise NoExecutableError unless File.exists?(PDFKit.configuration.wkhtmltopdf)
end

Class Attribute Details

.configurationObject

Configure PDFKit someplace sensible, like config/initializers/pdfkit.rb

Examples:

PDFKit.configure do |config|
  config.wkhtmltopdf = '/usr/bin/wkhtmltopdf'
  config.use_xvfb    = true
  config.verbose     = true
end

80
81
82
# File 'lib/pdfkit/configuration.rb', line 80

def configuration
  @configuration
end

Instance Attribute Details

#rendererObject (readonly)

Returns the value of attribute renderer


28
29
30
# File 'lib/pdfkit/pdfkit.rb', line 28

def renderer
  @renderer
end

#sourceObject

Returns the value of attribute source


27
28
29
# File 'lib/pdfkit/pdfkit.rb', line 27

def source
  @source
end

#stylesheetsObject

Returns the value of attribute stylesheets


27
28
29
# File 'lib/pdfkit/pdfkit.rb', line 27

def stylesheets
  @stylesheets
end

Class Method Details

.configure {|configuration| ... } ⇒ Object

Yields:


84
85
86
# File 'lib/pdfkit/configuration.rb', line 84

def self.configure
  yield(configuration)
end

Instance Method Details

#command(path = nil) ⇒ Object


46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/pdfkit/pdfkit.rb', line 46

def command(path = nil)
  args = @renderer.options_for_command
  shell_escaped_command = [executable, OS::shell_escape_for_os(args)].join ' '

  # In order to allow for URL parameters (e.g. https://www.google.com/search?q=pdfkit) we do
  # not escape the source. The user is responsible for ensuring that no vulnerabilities exist
  # in the source. Please see https://github.com/pdfkit/pdfkit/issues/164.
  input_for_command = @source.to_input_for_command
  output_for_command = path ? Shellwords.shellescape(path) : '-'

  "#{shell_escaped_command} #{input_for_command} #{output_for_command}"
end

#executableObject


64
65
66
# File 'lib/pdfkit/pdfkit.rb', line 64

def executable
  PDFKit.configuration.executable
end

#optionsObject


59
60
61
62
# File 'lib/pdfkit/pdfkit.rb', line 59

def options
  # TODO(cdwort,sigmavirus24): Replace this with an attr_reader for @renderer instead in 1.0.0
  @renderer.options
end

#to_file(path) ⇒ Object


86
87
88
89
# File 'lib/pdfkit/pdfkit.rb', line 86

def to_file(path)
  self.to_pdf(path)
  File.new(path)
end

#to_pdf(path = nil) ⇒ Object


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/pdfkit/pdfkit.rb', line 68

def to_pdf(path=nil)
  preprocess_html
  append_stylesheets

  invoke = command(path)

  result = IO.popen(invoke, "wb+") do |pdf|
    pdf.puts(@source.to_s) if @source.html?
    pdf.close_write
    pdf.gets(nil) if path.nil?
  end

  # $? is thread safe per
  # http://stackoverflow.com/questions/2164887/thread-safe-external-process-in-ruby-plus-checking-exitstatus
  raise ImproperWkhtmltopdfExitStatus, invoke if empty_result?(path, result) || !successful?($?)
  return result
end