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
- #device_pixel_ratio ⇒ Object
- #document_size ⇒ Object
-
#mhtml(path: nil) ⇒ Object
Saves MHTML on a disk or returns it as a string.
-
#pdf(**opts) ⇒ Object
Saves PDF on a disk or returns it as Base64.
-
#screenshot(**opts) ⇒ Object
Saves screenshot on a disk or returns it as base64.
- #viewport_size ⇒ Object
Instance Method Details
#device_pixel_ratio ⇒ Object
162 163 164 165 166 |
# File 'lib/ferrum/page/screenshot.rb', line 162 def device_pixel_ratio evaluate <<~JS window.devicePixelRatio JS end |
#document_size ⇒ Object
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.
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:
See other [native options](chromedevtools.github.io/devtools-protocol/tot/Page#method-printToPDF) you can pass.
Saves PDF on a disk or returns it as Base64.
132 133 134 135 136 137 |
# File 'lib/ferrum/page/screenshot.rb', line 132 def pdf(**opts) path, encoding = (**opts) = (**opts).merge(transferMode: "ReturnAsStream") handle = command("Page.printToPDF", **).fetch("stream") stream_to(path: path, encoding: encoding, handle: handle) end |
#screenshot(**opts) ⇒ Object
Saves screenshot on a disk or returns it as base64.
86 87 88 89 90 91 92 93 94 |
# File 'lib/ferrum/page/screenshot.rb', line 86 def screenshot(**opts) path, encoding = (**opts) = (path, **opts) data = capture_screenshot(, opts[:full], opts[:background_color]) return data if encoding == :base64 bin = Base64.decode64(data) save_file(path, bin) end |
#viewport_size ⇒ Object
156 157 158 159 160 |
# File 'lib/ferrum/page/screenshot.rb', line 156 def evaluate <<~JS [window.innerWidth, window.innerHeight] JS end |