October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk8 min

Convert a URL to PDF in Java Using Puppeteer

A practical Java-to-Puppeteer guide: run Puppeteer in Node.js from Java, control PDF rendering, troubleshoot failures, or use a hosted browser endpoint.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Puppeteer is a JavaScript library, not a native Java API. To convert a URL to PDF in a Java application, you can run Puppeteer in a separate Node.js process and have Java coordinate it, or call a hosted browser service from Java over HTTP. The first option gives you direct control over browser behavior; the second avoids managing Chromium yourself.

What Puppeteer does—and how Java fits in

Puppeteer automates Chrome and Firefox through browser automation protocols. Its PDF workflow renders a web page in a browser and saves the rendered result as a PDF. Java cannot call Puppeteer as though it were a Java library: the browser automation code runs in JavaScript, or Java sends an HTTP request to a service that runs it for you.

The local approach below uses Java’s ProcessBuilder to start a Node.js script. Java passes the URL and output filename as arguments, waits for the process to finish, and checks its exit code. This keeps the conversion callable from a Java application without pretending that Puppeteer runs in the JVM.

Option 1: Run Puppeteer in Node.js and call it from Java

Prerequisites

  • Install a current Node.js release and a Java version that provides java.net.http.HttpClient (Java 11 or later is a practical baseline for the Java example below).
  • Install Puppeteer in a project directory. Puppeteer’s installation may download a compatible browser; follow its installation output and ensure the runtime environment can launch that browser.
  • Make sure the Java process has permission to execute Node.js and write to the chosen output directory.

Install Puppeteer

From the directory where you will keep the script, run:

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
npm init -y
npm install puppeteer

Create the Puppeteer script

Save this as render-pdf.js in that directory. It takes the target URL and output path from the command line, waits for navigation to reach networkidle2, creates the PDF, and closes the browser even if rendering fails.

const puppeteer = require('puppeteer');

async function main() {
  const [url, outputPath] = process.argv.slice(2);
  if (!url || !outputPath) {
    throw new Error('Usage: node render-pdf.js <url> <output.pdf>');
  }

  const browser = await puppeteer.launch({ headless: true });
  try {
    const page = await browser.newPage();
    await page.goto(url, { waitUntil: 'networkidle2' });
    await page.pdf({ path: outputPath, format: 'A4', printBackground: true });
  } finally {
    await browser.close();
  }
}

main().catch((error) => {
  console.error(error);
  process.exitCode = 1;
});

Call the script from Java

This Java program starts Node with separate arguments rather than building a shell command string. That avoids shell-quoting problems when URLs contain query strings or special characters. It inherits the child process output so Puppeteer errors appear in the Java application’s logs.

import java.io.IOException;
import java.nio.file.Path;
import java.util.List;

public class UrlToPdf {
    public static void main(String[] args) throws IOException, InterruptedException {
        if (args.length != 2) {
            System.err.println("Usage: java UrlToPdf <url> <output.pdf>");
            System.exit(2);
        }

        String url = args[0];
        Path output = Path.of(args[1]).toAbsolutePath();
        Path script = Path.of("render-pdf.js").toAbsolutePath();

        Process process = new ProcessBuilder(
                "node", script.toString(), url, output.toString())
                .inheritIO()
                .start();

        int exitCode = process.waitFor();
        if (exitCode != 0) {
            throw new IOException("PDF conversion failed; Node exited with code " + exitCode);
        }
        System.out.println("PDF written to " + output);
    }
}

Compile and run from a directory where render-pdf.js is present:

javac UrlToPdf.java
java UrlToPdf "https://example.com" "example.pdf"

The Java process does not return until rendering ends. For a server handling many conversions, consider a worker queue or a managed pool of long-lived browser processes rather than starting a fresh browser for every request; that adds lifecycle and concurrency responsibilities, but avoids repeatedly paying browser startup overhead. Apply your own timeouts and concurrency limits so a slow or hostile page cannot occupy workers indefinitely.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Control the PDF’s appearance

Print styles or screen styles

page.pdf() uses the page’s print CSS media type by default. That means a site’s @media print rules can hide navigation, rearrange content, or change colors. If the PDF should resemble the screen layout instead, call await page.emulateMediaType('screen') after navigation and before page.pdf(). Choose based on the document you want, not on an assumption that PDF output matches a browser screenshot.

Page size, margins, orientation, and backgrounds

The example selects A4 paper and enables background printing. Adjust the PDF options for the document’s intended page size and layout. Margins and landscape orientation also affect pagination; test them against pages with long tables, wide content, or print-specific styling. Background printing can preserve colored panels and other background graphics that would otherwise be omitted.

Colors and fonts

Chromium applies print-oriented color adjustment by default, so exact on-screen colors are not guaranteed. When color fidelity matters, use CSS such as -webkit-print-color-adjust: exact in the page’s print styles and inspect the generated output. Puppeteer’s documented PDF flow waits for fonts to load by default, but a page can still render with a fallback if its font request fails or the site’s own loading behavior is incomplete.

Wait for the page’s actual content

networkidle2 is a useful starting point, not a universal definition of readiness. Pages with analytics, live updates, streaming requests, or delayed client-side rendering may not become idle at the right time. If a known element marks completion, wait for that selector with page.waitForSelector() before generating the PDF. A fixed delay can help with a known animation or delayed widget, but it can also waste time or still be too short; prefer a site-specific readiness condition where possible.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Option 2: Call a hosted PDF endpoint from Java

A hosted browser API lets Java send an HTTP request and receive PDF bytes without launching or patching Chromium in the application environment. Browserless documents a Java example using java.net.http.HttpClient: the client sends a JSON POST request with a URL and PDF settings, then writes the response bytes as a PDF. Its endpoint accepts either a URL or raw HTML and returns an application/pdf response.

This is an HTTP integration with a browser service, not Puppeteer running natively in Java. The service’s request format and options determine what you can control. Before adopting one, check its current authentication method, limits, pricing, data-handling terms, and supported PDF options; those vary by provider and account and should not be inferred from a code example.

  • Local Node/Puppeteer: your team owns browser installation, patching, scaling, and process recovery. You can use Puppeteer’s page APIs for interactions and custom readiness logic. Page content stays within infrastructure you control, subject to the target site and your network setup.
  • Hosted HTTP service: the provider operates the browser infrastructure, which reduces deployment work. You depend on the provider’s availability, API behavior, limits, and pricing, and the URL or HTML you submit is processed by that service. Confirm whether its controls suit your interaction and readiness needs.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Page ranges, metadata, and accessibility

Page ranges

If a hosted service lets you request selected PDF pages, ensure the ranges cover every page you intend to keep. Browserless documents that uncovered pages can be silently omitted and that out-of-range requests can produce an error. Validate the resulting page count when completeness matters.

PDF title and author

Puppeteer’s documented page.pdf() workflow does not provide built-in PDF metadata options such as title or author. If you need metadata, generate the PDF and then update it with a PDF library in a separate step; hosted services may also require a post-processing library for this.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Tagged PDFs and formal accessibility requirements

Browserless describes tagged output as structural information derived from the source markup and cautions that it is not certified PDF/UA output. A tagged PDF should not be treated as proof of formal accessibility compliance. If compliance is a requirement, validate the generated file with appropriate accessibility tools and remediate the source document and output as needed.

Troubleshooting

  • Java reports that Node could not start: Node may not be on the Java process’s PATH. Use the full path to the Node executable as the first ProcessBuilder argument, and verify the service account can execute it.
  • Puppeteer cannot launch Chromium: confirm installation completed, the browser binary is present, and the runtime environment has the libraries and permissions it requires. Container and server environments may need additional setup; use the official Puppeteer installation guidance for the specific deployment.
  • The PDF is blank or missing late content: navigation completion may occur before application rendering finishes. Wait for a meaningful selector or application readiness condition before calling page.pdf().
  • The page times out waiting for network idle: persistent requests can prevent the chosen readiness condition from being reached. Use a readiness signal appropriate to the page instead of assuming all pages become idle.
  • Colors or layout differ from the browser: PDF generation uses print media unless you emulate screen media. Check print CSS, background printing, margins, and color-adjustment rules.
  • Java says conversion failed: the example propagates a nonzero Node exit code. Read the inherited Node error output; it commonly identifies an invalid URL, browser launch failure, navigation problem, or unwritable destination.
  • A hosted PDF is missing pages or returns an error: check that requested page ranges are valid and collectively include the pages you expect.

Or skip the browser setup

ScreenshotNeo is a website screenshot API that can return screenshots or PDFs, with an MCP server for AI agents. Its clean-shot flow accepts cookie/consent banners and removes known consent platforms, newsletter popups, and chat widgets before capture; those steps can be turned off. Bot checks, blank pages, timeouts, failed loads, and cache hits are not billed, and responses identify page verdict and billing status. For PDF options, see the ScreenshotNeo documentation.

Example request (use a URL you are authorized to capture):

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://example.com -o shot.webp

That supplied example saves a WebP image; use the documentation for the PDF request settings. ScreenshotNeo includes 1,000 shots per month on its free plan with no card required, and paid plans start at $5 for 3,000 shots. Sign up for the free plan.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Frequently Asked Questions

Can I use Puppeteer directly from Java?

No. Puppeteer is a JavaScript library. Run it in a Node.js process that Java coordinates, or call a hosted browser API over HTTP.

Does Puppeteer make PDFs using print or screen styles?

Print CSS is the default for page.pdf(). Call page.emulateMediaType('screen') before PDF generation if you want screen media styles.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. Shenzhen desk3 min
    HONOR Expands Beyond Smartphones With Humanoid Robot RevealHONOR said it unveiled its first humanoid robot at MWC 2026 and named shopping assistance, workplace inspections, and supportive companionship as intended uses. Later Robotics D1 claims and a reported…
  2. Cupertino desk5 min
    Apple Unveils AirPods Max 2: The Upgrade That Should Have Happened Years AgoAirPods Max 2 adds H2-powered audio features and Apple claims up to 1.5× more effective ANC, but its design, Smart Case, and 20-hour battery rating are unchanged. Wired lossless audio…
  3. Cupertino desk4 min
    Apple’s OLED Touch MacBooks Are Coming—but the Dynamic Island Is the Real GambleApple has not announced an OLED touchscreen MacBook, but reports point to high-end models arriving in late 2026 or early 2027. The reported Mac Dynamic Island could be useful, but…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.