Module: Postdoc

Defined in:
lib/postdoc.rb

Class Method Summary collapse

Class Method Details

.render_from_string(string, options) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/postdoc.rb', line 8

def self.render_from_string(string, options)
  htmlfile = Tempfile.new ['input', '.html']

  htmlfile.write string
  htmlfile.flush

  # random port at 1025 or higher
  random_port = 1024 + Random.rand(65535 - 1024)

  pid = Process.spawn "chrome --remote-debugging-port=#{random_port} --headless"

  # FIXME
  sleep 1

  begin
    chrome = ChromeRemote.client port: random_port
    chrome.send_cmd 'Page.enable'

    chrome.send_cmd 'Page.navigate', url: "file://#{htmlfile.path}"
    chrome.wait_for 'Page.loadEventFired'

    if options[:header_template].present? || options[:footer_template].present?
      displayHeaderFooter = true
    else
      displayHeaderFooter = false
    end

    response = chrome.send_cmd 'Page.printToPDF', {
      landscape: options[:landscape] || false,
      printBackground: true,
      marginTop: options[:margin_top] || 1,
      marginBottom: options[:margin_bottom] || 1,
      displayHeaderFooter: displayHeaderFooter,
      headerTemplate: options[:header_template] || '',
      footerTemplate: options[:footer_template] || ''
    }
    result = Base64.decode64 response['data']
  ensure
    Process.kill 'KILL', pid
    Process.wait pid

    htmlfile.close
    htmlfile.unlink
  end

  result
end