Module: Ferrum::Page::Screenshot

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

Constant Summary collapse

FULL_WARNING =
"Ignoring :selector or :area in #screenshot since full: true was given at %s"
AREA_WARNING =
"Ignoring :area in #screenshot since selector: was given at %s"
DEFAULT_SCREENSHOT_FORMAT =
"png"
SUPPORTED_SCREENSHOT_FORMAT =
%w[png jpeg jpg webp].freeze
DEFAULT_PDF_OPTIONS =
{
  landscape: false,
  paper_width: 8.5,
  paper_height: 11,
  scale: 1.0
}.freeze
PAPER_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

Instance Method Summary collapse

Instance Method Details

#device_pixel_ratioObject



162
163
164
165
166
# File 'lib/ferrum/page/screenshot.rb', line 162

def device_pixel_ratio
  evaluate <<~JS
    window.devicePixelRatio
  JS
end

#document_sizeObject



168
169
170
171
172
173
# File 'lib/ferrum/page/screenshot.rb', line 168

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

#mhtml(path: nil) ⇒ Object

Saves MHTML on a disk or returns it as a string.

Examples:

page.go_to("https://google.com/")
page.mhtml(path: "google.mhtml") # => 87742

Parameters:

  • path (String, nil) (defaults to: nil)

    The path to save a file on the disk.



149
150
151
152
153
154
# File 'lib/ferrum/page/screenshot.rb', line 149

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

Note:

Saves PDF on a disk or returns it as Base64.

Examples:

page.go_to("https://google.com/")
# Save to disk as a PDF
page.pdf(path: "google.pdf", paper_width: 1.0, paper_height: 1.0) # => true

Parameters:

  • opts (Hash{Symbol => Object})

Options Hash (**opts):

  • :path (String)

    The path to save a screenshot on the disk. ‘:encoding` will be set to `:binary` automatically.

  • :encoding (:base64, :binary)

    The encoding the image should be returned in.

  • :landscape (Boolean) — default: false

    Page orientation.

  • :scale (Float)

    Zoom in/out.

  • :format (:letter, :legal, :tabloid, :ledger, :A0, :A1, :A2, :A3, :A4, :A5, :A6)

    The standard paper size.

  • :paper_width (Float)

    Sets the paper’s width.

  • :paper_height (Float)

    Sets the paper’s height.



132
133
134
135
136
137
# File 'lib/ferrum/page/screenshot.rb', line 132

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

#screenshot(**opts) ⇒ Object

Saves screenshot on a disk or returns it as base64.

Examples:

page.go_to("https://google.com/")

Save on the disk in PNG:

page.screenshot(path: "google.png") # => 134660

Save on the disk in JPG:

page.screenshot(path: "google.jpg") # => 30902

Save to Base64 in WebP with reduce quality:

page.screenshot(format: "webp", quality: 60) # "iVBORw0KGgoAAAANS...

Save to Base64 the whole page not only viewport and reduce quality:

page.screenshot(full: true, format: "jpeg", quality: 60) # "iVBORw0KGgoAAAANS...

Save with specific background color:

page.screenshot(background_color: Ferrum::RGBA.new(0, 0, 0, 0.0))

Parameters:

  • opts (Hash{Symbol => Object})

Options Hash (**opts):

  • :path (String)

    The path to save a screenshot on the disk. ‘:encoding` will be set to `:binary` automatically.

  • :encoding (:base64, :binary)

    The encoding the image should be returned in.

  • :format ("jpeg", "jpg", "png", "webp")

    The format the image should be returned in.

  • :quality (Integer)

    The image quality. Note: 0-100 works for jpeg only.

  • :full (Boolean)

    Whether you need full page screenshot or a viewport.

  • :selector (String)

    CSS selector for the given element.

  • :area (Hash)

    x, y, width, height to screenshot an area.

  • :scale (Float)

    Zoom in/out.

  • :background_color (Ferrum::RGBA)

    Sets the background color.



86
87
88
89
90
91
92
93
94
# File 'lib/ferrum/page/screenshot.rb', line 86

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



156
157
158
159
160
# File 'lib/ferrum/page/screenshot.rb', line 156

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