Back to Resources

Blog

Posted October 14, 2021

How to Print Page as PDF with Selenium 4

Selenium 4 allows users to print a page as a PDF through Chrome, Firefox, and Edge.

quote

The following Java example will show how a url is loaded and then printed as a PDF. For additional examples in multiple languages, check our our Selenium 4 Documentation.

Firefox

Something important to note is that GeckoDriver 0.30.0 or higher is needed to use Firefox with Selenium 4. At the moment, Firefox 93 (beta) is needed.

1
@Test
2
public void printPageWithFirefox() throws IOException {
3
String userName = System.getenv("SAUCE_USERNAME");
4
String accessKey = System.getenv("SAUCE_ACCESS_KEY");
5
URL gridUrl = new URL("https://ondemand.us-west-1.saucelabs.com:443/wd/hub");
6
FirefoxOptions firefoxOptions = new FirefoxOptions();
7
firefoxOptions.setCapability("platformName", "Windows 10");
8
firefoxOptions.setCapability("browserVersion", "beta");
9
10
Map<String, Object> sauceOptions = new HashMap<>();
11
sauceOptions.put("name", "printPageWithFirefox");
12
sauceOptions.put("username", userName);
13
sauceOptions.put("accessKey", accessKey);
14
sauceOptions.put("geckodriverVersion", "0.30.0");
15
firefoxOptions.setCapability("sauce:options", sauceOptions);
16
17
RemoteWebDriver driver = new RemoteWebDriver(gridUrl, firefoxOptions);
18
Path printPage = Paths.get("src/test/screenshots/PrintPageFirefox.pdf");
19
20
driver.get("https://www.saucedemo.com/v1/inventory.html");
21
Pdf print = driver.print(new PrintOptions());
22
Files.write(printPage, OutputType.BYTES.convertFromBase64Png(print.getContent()));
23
driver.quit();
24
}

Chrome

1
@Test
2
public void printPageWithChrome() throws IOException {
3
String userName = System.getenv("SAUCE_USERNAME");
4
String accessKey = System.getenv("SAUCE_ACCESS_KEY");
5
URL gridUrl = new URL("https://ondemand.us-west-1.saucelabs.com:443/wd/hub");
6
ChromeOptions chromeOptions = new ChromeOptions();
7
// PrintToPDF is only supported in headless mode
8
chromeOptions.setHeadless(true);
9
chromeOptions.setCapability("platformName", "Windows 10");
10
chromeOptions.setCapability("browserVersion", "latest");
11
12
Map<String, Object> sauceOptions = new HashMap<>();
13
sauceOptions.put("name", "printPageWithChrome");
14
sauceOptions.put("username", userName);
15
sauceOptions.put("accessKey", accessKey);
16
chromeOptions.setCapability("sauce:options", sauceOptions);
17
18
RemoteWebDriver driver = new RemoteWebDriver(gridUrl, chromeOptions);
19
Path printPage = Paths.get("src/test/screenshots/PrintPageChrome.pdf");
20
21
driver.get("https://www.saucedemo.com/v1/inventory.html");
22
Pdf print = driver.print(new PrintOptions());
23
Files.write(printPage, OutputType.BYTES.convertFromBase64Png(print.getContent()));
24
driver.quit();
25
}

Titus Fortner
Sr. Developer Experience Engineer, Sauce Labs
Diego Molina
Staff Software Engineer at Sauce Labs
Published:
Oct 14, 2021
Share this post
Copy Share Link
© 2023 Sauce Labs Inc., all rights reserved. SAUCE and SAUCE LABS are registered trademarks owned by Sauce Labs Inc. in the United States, EU, and may be registered in other jurisdictions.