Generate a PDF in Java

The simplest way to generate a PDF in Java is to POST your HTML to the Docweave API and write the returned bytes to disk — no headless browser to install or manage, and no PDF library on your classpath.

Generate a PDF with java.net.http.HttpClient

Java 11+ ships java.net.http.HttpClient in the standard library, so this is a self-contained example with no extra dependencies. It POSTs HTML to /api/v1/pdf, reads the response as bytes, and writes output.pdf:

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpRequest.BodyPublishers;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
import java.nio.file.Files;
import java.nio.file.Path;

public class GeneratePdf {

    public static void main(String[] args) throws Exception {
        String html = "<h1>Hello from Java</h1><p>Rendered by Docweave.</p>";

        String requestBody = "{"
                + "\"source\": {"
                + "\"type\": \"html\","
                + "\"html\": \"" + html.replace("\"", "\\\"") + "\""
                + "},"
                + "\"options\": { \"format\": \"A4\" },"
                + "\"idempotencyKey\": \"java-quickstart-001\""
                + "}";

        HttpClient client = HttpClient.newHttpClient();

        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create("https://docweave.dev/api/v1/pdf"))
                .header("Authorization", "Bearer dw_live_your_key")
                .header("Content-Type", "application/json")
                .header("Accept", "application/pdf")
                .POST(BodyPublishers.ofString(requestBody))
                .build();

        HttpResponse<byte[]> response = client.send(request, BodyHandlers.ofByteArray());

        if (response.statusCode() == 200) {
            Files.write(Path.of("output.pdf"), response.body());
            System.out.println("Saved output.pdf (" + response.body().length + " bytes)");
        } else {
            String errorBody = new String(response.body());
            System.err.println("Docweave API error " + response.statusCode() + ": " + errorBody);
        }
    }
}

Swap dw_live_your_key for your real API key, and swap source.type: "html" for source.type: "url" or source.type: "template" if you'd rather render an existing page or bind JSON into a stored template.

…or from an AI agent

The same rendering pipeline is exposed as an MCP tool via npx @docweave/mcp. If your Java service is fronted or orchestrated by an AI agent, the agent can call generate_pdf directly instead of shelling out to your Java code or building the HTTP request itself:

// From an AI agent over MCP — no HTTP client, no JSON escaping:
generate_pdf({
  source: {
    type: "html",
    html: "<h1>Hello from Java</h1><p>Rendered by Docweave.</p>"
  },
  outputPath: "/tmp/output.pdf"
})

FAQ

How do I generate a PDF from Java without Puppeteer or a headless browser?

Call the Docweave API instead of driving a browser yourself. POST your HTML (or a URL, or a template + JSON) to /api/v1/pdf and Docweave renders it with Chromium on its side, returning PDF bytes in the response — your Java process never has to launch or maintain a browser.

Does the Docweave API work with java.net.http.HttpClient?

Yes. Docweave is a plain REST endpoint, so java.net.http.HttpClient (built into Java 11+) is enough: send an HttpRequest with an Authorization: Bearer header and a JSON body, then read the response body as bytes and write it to a file.

How do I convert HTML to PDF in Java?

Send the HTML string as source.type: "html" in the request body to POST /api/v1/pdf. Docweave renders the HTML with a real Chromium engine and returns the finished PDF, so there's no HTML-to-PDF library to add to your classpath.

How is PDF generation from Java billed, and how do I avoid duplicate charges on retry?

Docweave bills per document, not per page, so a long report costs the same as a short one. Pass an idempotencyKey (for example an order or job ID) with each request — a retried call with the same key returns the original result instead of rendering and billing again.

Ready to generate PDFs from your Java app?

Get an API key