Class: Postdoc::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/postdoc/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(port) ⇒ Client

Returns a new instance of Client.



9
10
11
12
13
# File 'lib/postdoc/client.rb', line 9

def initialize(port)
  @port = port
  100.times { setup_connection_or_wait && break }
  raise 'ChromeClient couldn\'t launch' if @client.blank?
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



7
8
9
# File 'lib/postdoc/client.rb', line 7

def client
  @client
end

Instance Method Details



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/postdoc/client.rb', line 15

def print_pdf_from_html(file_path,
    header_template: false,
    footer_template: false,
    **options)

  client.send_cmd 'Page.enable'
  client.send_cmd 'Page.navigate', url: "file://#{file_path}"
  client.wait_for 'Page.loadEventFired'

  response = client.send_cmd 'Page.printToPDF', {
    landscape: options[:landscape] || false,
    printBackground: true,
    marginTop: options[:margin_top] || 1,
    marginBottom: options[:margin_bottom] || 1,
    marginLeft: options[:margin_left] || 1,
    marginRight: options[:margin_right] || 1,
    displayHeaderFooter: !!(header_template || footer_template),
    headerTemplate: header_template || '',
    footerTemplate: footer_template || ''
  }

  Base64.decode64 response['data']
end