Module: Ferrum::Page::Screenshot
- Included in:
- Ferrum::Page
- Defined in:
- lib/ferrum/page/screenshot.rb
Overview
Captures screenshots, PDFs, and MHTML snapshots of the page, and exposes viewport/document size helpers used to compute capture areas.
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_RENDER_TIMEOUT =
60- 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
-
#device_pixel_ratio ⇒ Float
The ratio of the resolution in physical pixels to the resolution in CSS pixels for the current display device.
-
#document_size ⇒ (Integer, Integer)
Full size of the document, including the part that is not visible in the viewport.
-
#mhtml(path: nil) ⇒ Object
Saves MHTML on a disk or returns it as a string.
-
#pdf(timeout: DEFAULT_RENDER_TIMEOUT, **opts) ⇒ Object
Saves PDF on a disk or returns it as Base64.
-
#screenshot(timeout: DEFAULT_RENDER_TIMEOUT, **opts) ⇒ Object
Saves screenshot on a disk or returns it as base64.
-
#viewport_size ⇒ (Integer, Integer)
Current viewport size.
Instance Method Details
#device_pixel_ratio ⇒ Float
The ratio of the resolution in physical pixels to the resolution in CSS pixels for the current display device.
195 196 197 198 199 |
# File 'lib/ferrum/page/screenshot.rb', line 195 def device_pixel_ratio evaluate " window.devicePixelRatio\n JS\nend\n" |
#document_size ⇒ (Integer, Integer)
Full size of the document, including the part that is not visible in the viewport.
211 212 213 214 215 216 |
# File 'lib/ferrum/page/screenshot.rb', line 211 def document_size evaluate " [document.documentElement.scrollWidth,\n document.documentElement.scrollHeight]\n JS\nend\n" |
#mhtml(path: nil) ⇒ Object
Saves MHTML on a disk or returns it as a string.
164 165 166 167 168 169 |
# File 'lib/ferrum/page/screenshot.rb', line 164 def mhtml(path: nil) data = command("Page.captureSnapshot", format: :mhtml).fetch("data") return data if path.nil? save_file(path, data) end |
#pdf(timeout: DEFAULT_RENDER_TIMEOUT, **opts) ⇒ Object
See other native options you can pass.
Saves PDF on a disk or returns it as Base64.
147 148 149 150 151 152 |
# File 'lib/ferrum/page/screenshot.rb', line 147 def pdf(timeout: DEFAULT_RENDER_TIMEOUT, **opts) path, encoding = (**opts) = (**opts).merge(transferMode: "ReturnAsStream") handle = command("Page.printToPDF", timeout: timeout, **).fetch("stream") stream_to(path: path, encoding: encoding, handle: handle) end |
#screenshot(timeout: DEFAULT_RENDER_TIMEOUT, **opts) ⇒ Object
Saves screenshot on a disk or returns it as base64.
96 97 98 99 100 101 102 103 104 |
# File 'lib/ferrum/page/screenshot.rb', line 96 def screenshot(timeout: DEFAULT_RENDER_TIMEOUT, **opts) path, encoding = (**opts) = (path, **opts) data = capture_screenshot(, opts[:full], opts[:background_color], timeout) return data if encoding == :base64 bin = Base64.decode64(data) save_file(path, bin) end |
#viewport_size ⇒ (Integer, Integer)
Current viewport size.
180 181 182 183 184 |
# File 'lib/ferrum/page/screenshot.rb', line 180 def evaluate " [window.innerWidth, window.innerHeight]\n JS\nend\n" |