Published on

Convert HTML to PDF Using Playwright Java

Generating PDFs from HTML is a common requirement in modern Java web applications—whether it's for invoices, reports, or export features in admin panels.

In this guide, you'll learn how to convert HTML content to a PDF using Playwright for Java with a headless Chromium browser. We'll also introduce a fully-managed alternative for developers who want to avoid infrastructure overhead: LetMePDF.com.


🎯 Why Playwright?

Playwright is a Node- and Java-compatible browser automation library by Microsoft. It's primarily used for testing, but it's also perfect for PDF generation since it can render complex HTML/CSS and run headlessly.

🛠️ Getting Started

1. Add Playwright Java Dependency

Add the following dependency to your pom.xml if you're using Maven:

<dependency>
  <groupId>com.microsoft.playwright</groupId>
  <artifactId>playwright</artifactId>
  <version>1.44.0</version>
</dependency>

2. Install Playwright Browsers

You only need to run this once:

mvn exec:java -e -Dexec.mainClass=com.microsoft.playwright.CLI -Dexec.args="install"

This installs the required Chromium binaries.

🧪 HTML to PDF Conversion Example

Here's a minimal Java example that converts HTML content to PDF using Playwright:

import com.microsoft.playwright.*;

import java.nio.file.Paths;

public class HtmlToPdfExample {
    public static void main(String[] args) {
        try (Playwright playwright = Playwright.create()) {
            Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(true));
            BrowserContext context = browser.newContext();
            Page page = context.newPage();

            // Load HTML content
            String htmlContent = """
                <html>
                  <head>
                    <style>
                      body { font-family: Arial; padding: 40px; }
                      h1 { color: #ff4d4f; }
                    </style>
                  </head>
                  <body>
                    <h1>Hello from LetMePDF!</h1>
                    <p>This PDF was generated using Playwright and Java.</p>
                  </body>
                </html>
            """;

            page.setContent(htmlContent);

            // Export to PDF
            page.pdf(new Page.PdfOptions()
                .setPath(Paths.get("output.pdf"))
                .setFormat("A4"));

            System.out.println("✅ PDF generated as output.pdf");
        }
    }
}

This script will render the HTML in a headless Chromium instance and save it as a PDF called output.pdf.

🧼 Want to Avoid Managing Infrastructure?

If you'd rather not deal with:

  • Installing Chromium
  • Managing headless browser processes
  • Handling containerization and scaling
  • Then LetMePDF.com is built for you.

With LetMePDF, you can:

  • Convert HTML, URLs, and markdown to PDF with a single API call
  • Embed images, fonts, and styles reliably
  • Skip the browser setup entirely Here’s a basic API example using curl:
curl -X POST https://api.letmepdf.com/html-to-pdf \
  -H "Content-Type: application/json" \
  -d '{
    "html": "<h1>Hello from LetMePDF API!</h1><p>Easy and fast PDF generation</p>"
  }' --output output.pdf

No browser installs. No memory bloat. Just results.

✅ Conclusion

For developers who need maximum control and don't mind managing infrastructure, Playwright Java is an excellent open-source solution for HTML-to-PDF conversion. However, for teams that prioritize simplicity, scalability, and minimal maintenance, the LetMePDF.com API provides a compelling alternative.

Try the LetMePDF API today with our free tier and simplify your PDF generation workflow!

Further Reading:

Playwright Java Documentation LetMePDF API Reference