1. Overview

In this tutorial, we’ll explore how to generate a PDF file from a web page using the print() method available in the ChromeDriver class of Selenium 4. The print() method provides a straightforward way to capture web content directly to a PDF file.

We’ll look at generating PDFs using Chrome and Firefox browsers and demonstrate how to customize the PDF output using the PrintOptions class. This includes adjusting parameters such as orientation, page size, scale, and margins to tailor the PDF to specific requirements.

Moreover, our practical examples will involve printing the webpage’s contents using Java and JUnit tests.

2. Setup and Configuration

Two dependencies are required to configure the environment: Selenium Java and WebDriverManager. Selenium Java provides the necessary automation framework to interact with and control web browsers programmatically.

WebDriverManager simplifies the management of browser drivers by automatically handling their downloading and configuration. This setup is critical for smoothly executing our automated test and web interaction.

Let’s add the Maven dependencies:

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>4.23.1</version>
</dependency>
<dependency>
    <groupId>io.github.bonigarcia</groupId>
    <artifactId>webdrivermanager</artifactId>
    <version>5.8.0</version>
dependency>

3. PDF Generation With Chrome and Selenium

In this section, we’ll showcase how to convert a web page into PDF files using Selenium WebDriver with Chrome. We aim to capture the content of Baeldung’s Java Weekly updates and save it as a PDF document.

Let’s write a JUnit test to create a PDF file named Baeldung_Weekly.pdf in our project’s root directory:

@Test
public void whenNavigatingToBaeldung_thenPDFIsGenerated() throws IOException {
    ChromeDriver driver = new ChromeDriver();
    driver.get("https://www.baeldung.com/library/java-web-weekly");
    Pdf pdf = driver.print(new PrintOptions());
    byte[] pdfContent = Base64.getDecoder().decode(pdf.getContent());
    Files.write(Paths.get("./Baeldung_Weekly.pdf"), pdfContent);
    assertTrue(Files.exists(Paths.get("./Baeldung_Weekly.pdf")), "PDF file should be created");
    driver.quit();
}

Upon running the test, ChromeDriver opens the webpage and uses the print() method to create a PDF. The system decodes the Base64-encoded PDF and saves it as Baeldung_Weekly.pdf. It then checks for the file’s existence to confirm successful PDF generation. Finally, the browser is closed using the driver.quit() method*.* It’s always important to close the browser to ensure that no resources are left hanging.

Additionally, the print() method works seamlessly even when Chrome is running in headless mode, which is a mode where the browser operates without a GUI.

We can enable headless mode using options.addArguments(“–headless”) method to run the browser in the background without the GUI:

ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");

ChromeDriver driver = new ChromeDriver(options);

4. PDF Generation With Firefox and Selenium

The print() method is also supported in the Firefox browser. Let’s explore how to automate PDF generation using Firefox and Selenium WebDriver. Like before, the goal is to capture the page provided in the URL and save it as a PDF document using Firefox.

Let’s write a JUnit test to generate a PDF file named Firefox_Weekly.pdf in the project’s current directory:

@Test
public void whenNavigatingToBaeldungWithFirefox_thenPDFIsGenerated() throws IOException {
    FirefoxDriver driver = new FirefoxDriver(new FirefoxOptions());
    driver.get("https://www.baeldung.com/library/java-web-weekly");
    Pdf pdf = driver.print(new PrintOptions());
    byte[] pdfContent = Base64.getDecoder().decode(pdf.getContent());
    Files.write(Paths.get("./Firefox_Weekly.pdf"), pdfContent);
    assertTrue(Files.exists(Paths.get("./Firefox_Weekly.pdf")), "PDF file should be created");
    driver.quit();
}

The test executes, initiating the FirefoxDriver, which opens and navigates to the designated web address. The print() method then generates a PDF from the web page. The system encodes the resulting PDF in Base64, decodes it to binary format, and stores it as Firefox_Weekly.pdf.

The test confirms the PDF’s creation by checking its presence in the file system. This validation ensures we successfully generated a PDF file from a web page using the Firefox browser.

5. Customizing PDF Output With PrintOptions

When using the print() method, we can decide how the output PDF document should look. In this section, we’ll see how to enhance the capabilities of the print() method in Selenium WebDriver by customizing the PDF output using the PrintOptions class.

PrintOptions class is part of Selenium’s API and allows for detailed adjustments to a web page when rendered to PDF. Let’s learn a few of the many options provided by PrintOptions – orientation, page size, scale, and margins:

PrintOptions options = new PrintOptions();
        
options.setOrientation(PrintOptions.Orientation.LANDSCAPE);
options.setScale(1.5);
options.setPageSize(new PageSize(100, 100));
options.setPageMargin(new PageMargin(2, 2, 2, 2));

Pdf pdf = driver.print(options);

In the snippet, the PrintOptions class customizes the PDF output generated by the print() method. setOrientation() sets the page orientation, setScale() adjusts the content size, setPageSize() specifies a custom page size, and setPageMargin() defines the margins for each side.

6. Conclusion

In this article, we’ve walked through generating PDF files from web pages using Selenium 4’s print() method. We’ve demonstrated the ability to implement the same functionality across different platforms by trying print() on Chrome and Firefox.

Additionally, we explored the customization options available through the PrintOptions class, which allows for tailoring the output PDF document to meet specific requirements.