Module: Async::WebDriver::Scope::Printing

Included in:
Element, Async::WebDriver::Session
Defined in:
lib/async/webdriver/scope/printing.rb

Overview

Helpers for printing the current page to PDF.

Instance Method Summary collapse

Instance Method Details

Print the current page as a PDF and return the raw binary data.

All margin and page measurements are in centimetres. The W3C WebDriver default page size is US Letter (21.59 × 27.94 cm) with 1 cm margins.



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
# File 'lib/async/webdriver/scope/printing.rb', line 26

def print(orientation: nil, scale: nil, background: nil, page: nil, margin: nil, page_ranges: nil, shrink_to_fit: nil)
	parameters = {
		orientation: orientation,
		scale: scale,
		background: background,
		page: page,
		margin: margin,
		pageRanges: page_ranges,
		shrinkToFit: shrink_to_fit,
	}.compact
	
	# Synchronise with Chrome's rendering pipeline before issuing the print
	# command. The underlying CDP call (Page.printToPDF) is synchronous: if
	# the renderer process has not yet fully initialised its print pipeline
	# by the time the command arrives, Chrome returns JSON-RPC error -32000
	# ("Printing failed") with no retry. A JavaScript round-trip forces
	# ChromeDriver to wait for the renderer to be live (a JS execution
	# context must exist), which also guarantees the print pipeline is ready.
	# Without this, fast-loading pages can trigger the race intermittently.
	session.execute("return document.readyState")
	
	reply = session.post("print", parameters)
	
	return Base64.decode64(reply)
end