Dev.to WebDev 🛠 Dev 👁 2 📖 4 min read

Save a Web Page as PDF in Java: Playwright and IronPDF

Why does the PDF look different from the page in the browser? Teams ask that after they save a web page as PDF in Java with a headless browser and open the file. The monthly statement from the account portal comes out wi

Why does the PDF look different from the page in the browser? Teams ask that after they save a web page as PDF in Java with a headless browser and open the file. The monthly statement from the account portal comes out with its summary rearranged, its colored header band gone white or a lazy-loaded logo missing.

Playwright for Java, Apache 2.0 licensed and published by Microsoft to Maven Central, drives Chromium, Firefox and WebKit from one API. We trace the differences to its print defaults and load timing, and then render the same statement with IronPDF for Java.

What does Playwright's print call do?

It prints the page, the way a print dialog would. Saving a statement takes one call.

// Print the page to a PDF file, as a print dialog would
page.pdf(new Page.PdfOptions().setPath(Paths.get("statement-october.pdf")));

page.pdf() generates the file with print CSS media, so every @media print rule in the portal's stylesheet, which the screen ignores, applies to the PDF. A statement page that stacks its two-column summary for print is why the PDF looks like a different page. Colors are also adjusted for printing unless the page's CSS forces exact ones with -webkit-print-color-adjust, and printBackground defaults to false, which explains a white header band.

Each default has a switch. Screen rendering takes page.emulateMedia with Media.SCREEN before the print, and backgrounds take setPrintBackground(true). Headers and footers follow their own rule, since script tags inside their templates are not evaluated.

Why do images go missing from some statements?

Because some images and canvases are drawn after the page first loads. Two closed reports on the project's tracker describe PDFs that did not match the page, one about images missing from the printed file and one about lost CSS. On the images report, a maintainer reproduced the problem on Linux and read it as a Chromium bug, and the report was later closed as stale with no fix version attached.

Other users added the useful part over the following years. For one, the images were lazy-loaded, and scrolling to the bottom of the page before printing brought them in, and for another, a canvas was not drawn until the code waited for the network to go idle. A statement that loads its transaction table after first paint is that kind of page, so the print has to wait until the table is drawn.

The print options Playwright offers

The print call is well equipped, with paper format, width and height, margins, orientation, page ranges, scale, backgrounds and CSS page size. Header and footer templates get classes for the date, title, URL, page number and total pages, and the Java API has carried outline and tagged options since v1.42. Maven Central received version 1.65.2 on December 1, 2026, and the system requirements list Java 8 or higher with Windows, macOS and Linux.

Services carry one more rule. The multithreading guide says Playwright for Java is not thread safe, so its objects belong to the thread where the Playwright object was created, and a shared request pool needs an instance per worker or a lock.

Screen media, backgrounds and a wait in IronPDF for Java

Here those three settings are render options on one call. IronPDF for Java takes the statement's URL and returns a PdfDocument, and its PDF generation settings hold the options that decide how the page prints.

import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.render.ChromePdfRenderOptions;
import com.ironsoftware.ironpdf.render.CssMediaType;

import java.nio.file.Paths;

public class OctoberStatement {
    public static void main(String[] args) throws Exception {
        // Create the render options for the statement
        ChromePdfRenderOptions options = new ChromePdfRenderOptions();

        // Print with the statement's screen styling and background colors
        options.setCssMediaType(CssMediaType.SCREEN);
        options.setPrintHtmlBackgrounds(true);

        // Wait briefly so the transaction table finishes loading before the render
        options.getWaitFor().RenderDelay(1000);

        // Render the statement from its URL and save it
        PdfDocument statement = PdfDocument.renderUrlAsPdf("https://portal.example.com/statements/october", options);
        statement.saveAs(Paths.get("statement-october.pdf"));
    }
}

The three settings that Playwright's defaults leave to the caller sit together in one options object, so a statement that matches the screen is a single, reviewable configuration. IronPDF's URL-to-PDF example covers the call, and a portal page behind a password prompt can be reached with the login credentials that renderUrlAsPdf accepts.

Footers can carry the portal's branding. The statement footer is its own HTML footer with {page} and {total-pages} placeholders, and its markup can set the portal's fonts and colors. Statements hold account details, so IronPDF can apply passwords and permissions before each file is emailed to the customer.

Rolling the change across the statement jobs

Start with the statements whose PDF already differs from the tab, rendered with screen media and backgrounds. Pages with lazy-loaded images or late tables come next, with the delay tuned to the slowest section, and statements that need a branded footer follow with the footer written as HTML.

Pointed at a local test page, the listing ran under com.ironsoftware:ironpdf 2026.11.3 and Java 17 in the run of 27 November 2026, and the PDF kept a screen-only line that the page hides for print, the blue header band and a transaction table that the page added 400 ms after loading. The license key guide for Java shows where a trial key goes before the first statement renders.

Playwright, Microsoft, Chromium, Firefox, WebKit and Java are trademarks of their respective owners, we have no affiliation with any of them, and the facts above are based on the Playwright for Java documentation, repository and issues, Maven Central records and IronPDF for Java documentation.

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.