Module: Ferrum::Page::Screenshot

Included in:
Ferrum::Page
Defined in:
lib/ferrum/page/screenshot.rb

Constant Summary collapse

DEFAULT_PDF_OPTIONS =
{
  landscape: false,
  paper_width: 8.5,
  paper_height: 11,
  scale: 1.0
}.freeze
PAPEP_FORMATS =
{
  letter:   { width:  8.50, height: 11.00 },
  legal:    { width:  8.50, height: 14.00 },
  tabloid:  { width: 11.00, height: 17.00 },
  ledger:   { width: 17.00, height: 11.00 },
  A0:       { width: 33.10, height: 46.80 },
  A1:       { width: 23.40, height: 33.10 },
  A2:       { width: 16.54, height: 23.40 },
  A3:       { width: 11.70, height: 16.54 },
  A4:       { width:  8.27, height: 11.70 },
  A5:       { width:  5.83, height:  8.27 },
  A6:       { width:  4.13, height:  5.83 },
}.freeze
STREAM_CHUNK =
128 * 1024

Instance Method Summary collapse

Instance Method Details

#document_sizeObject



65
66
67
68
69
70
# File 'lib/ferrum/page/screenshot.rb', line 65

def document_size
  evaluate <<~JS
    [document.documentElement.scrollWidth,
     document.documentElement.scrollHeight]
  JS
end

#mhtml(path: nil) ⇒ Object



53
54
55
56
57
# File 'lib/ferrum/page/screenshot.rb', line 53

def mhtml(path: nil)
  data = command("Page.captureSnapshot", format: :mhtml).fetch("data")
  return data if path.nil?
  save_file(path, data)
end

#pdf(**opts) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ferrum/page/screenshot.rb', line 41

def pdf(**opts)
  path, encoding = common_options(**opts)
  options = pdf_options(**opts).merge(transferMode: "ReturnAsStream")
  handle = command("Page.printToPDF", **options).fetch("stream")

  if path
    stream_to_file(handle, path: path)
  else
    stream_to_memory(handle, encoding: encoding)
  end
end

#screenshot(**opts) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/ferrum/page/screenshot.rb', line 31

def screenshot(**opts)
  path, encoding = common_options(**opts)
  options = screenshot_options(path, **opts)
  data = capture_screenshot(options, opts[:full], opts[:background_color])
  return data if encoding == :base64

  bin = Base64.decode64(data)
  save_file(path, bin)
end

#viewport_sizeObject



59
60
61
62
63
# File 'lib/ferrum/page/screenshot.rb', line 59

def viewport_size
  evaluate <<~JS
    [window.innerWidth, window.innerHeight]
  JS
end