Class: WkhtmltopdfRunner::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/wkhtmltopdf_runner/runner.rb

Instance Method Summary collapse

Instance Method Details

#configObject Also known as: configuration



43
44
45
# File 'lib/wkhtmltopdf_runner/runner.rb', line 43

def config
  @config ||= Configuration.new
end

#configure {|config| ... } ⇒ Object

Configure client with a block of settings.

Yields:



49
50
51
52
# File 'lib/wkhtmltopdf_runner/runner.rb', line 49

def configure
  yield(config) if block_given?
  true
end

#pdf_from_file(file, options = {}, &block) ⇒ Object



24
25
26
# File 'lib/wkhtmltopdf_runner/runner.rb', line 24

def pdf_from_file(file, options = {}, &block)
  pdf_from_url("file://#{file.path}", options, &block)
end

#pdf_from_string(string, options = {}, &block) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/wkhtmltopdf_runner/runner.rb', line 28

def pdf_from_string(string, options = {}, &block)
  Tempfile.open(['file-', '.html']) do |html_file|
    html_file.write(string)
    html_file.rewind

    pdf_from_file(html_file, options, &block)
  end
end

#pdf_from_url(url, options = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/wkhtmltopdf_runner/runner.rb', line 8

def pdf_from_url(url, options = {})
  Tempfile.open(['file-', '.pdf']) do |pdf_file|
    pdf_file.binmode

    run(url, pdf_file, options)
    pdf_file.rewind

    if block_given?
      yield(pdf_file)
      return true
    end

    pdf_file.read
  end
end

#run(url, file, options = {}) ⇒ Object



37
38
39
40
41
# File 'lib/wkhtmltopdf_runner/runner.rb', line 37

def run(url, file, options = {})
  WkhtmltopdfRunner::Cmd
    .new(url: url, file: file, config: config, options: options)
    .run
end