FrameForge › Guides › HTML to PDF in Java

HTML to PDF in Java

Send HTML (or a URL) to the FrameForge API and get a PDF back. It is rendered in real Chromium, so modern CSS, web fonts and JavaScript all work. No browser to install or babysit.

The code

Java 11+ (java.net.http). Set FRAMEFORGE_KEY to your API key.

import java.net.URI;
import java.net.http.*;
import java.nio.file.Path;
import java.time.Duration;

public class RenderPdf {
  public static void main(String[] args) throws Exception {
    String json = "{\"html\":\"<h1>Invoice #1001</h1>\",\"format\":\"pdf\",\"width\":794,\"height\":1123}";
    HttpRequest req = HttpRequest.newBuilder(URI.create("https://api.frameforgeapi.com/render"))
        .header("Content-Type", "application/json")
        .header("X-API-Key", System.getenv("FRAMEFORGE_KEY"))
        .timeout(Duration.ofSeconds(60))
        .POST(HttpRequest.BodyPublishers.ofString(json))
        .build();
    HttpResponse<Path> res = HttpClient.newHttpClient()
        .send(req, HttpResponse.BodyHandlers.ofFile(Path.of("invoice.pdf")));
    if (res.statusCode() != 200) throw new RuntimeException("Render failed: " + res.statusCode());
  }
}

How it works

Rendering a URL instead of HTML

{"url": "https://example.com/report", "format": "pdf", "width": 1280, "height": 800, "fullPage": true}

Doing it yourself instead

Playwright for Java runs Chromium locally, and OpenHTMLtoPDF renders XHTML with CSS 2.1-level support.

An API makes sense when you don't want Chromium on your servers, or when you need images (PNG/JPEG) and PDFs from the same call. If you render a few PDFs a month on a server you already run, self-hosting is perfectly reasonable.

Free tool: Website to PDF. Other languages: Node.js · Python · PHP · Ruby · Go · C# / .NET · curl / Bash · Next.js · Laravel · Django · Ruby on Rails