· tutorials · 11 min read

Updated on

How To Convert HTML to PDF with Java

Discover the most efficient methods to convert HTML to PDF using Java and select the best tool for your needs

Summarize with AI:
Discover the most efficient methods to convert HTML to PDF using Java and select the best tool for your needs

Introduction

Generating PDFs from HTML in Java is rarely just about converting a file. It’s about consistency. You need your invoices to look identical across environments. Your reports must render correctly in production. Your CSS should behave the same way every time. But in reality, different rendering engines support different standards, dependencies become painful to manage, and scaling document generation adds unexpected complexity.

If you’re building document workflows in Java, whether for billing systems, SaaS dashboards, contracts, or enterprise reporting, converting HTML to a stable, production-ready PDF is a critical requirement.

In this article, you’ll discover the most effective methods for converting HTML to PDF using Java. I’ll discuss the most commonly used libraries, how they function, their strengths, and their limitations.

Additionally, I will introduce a production-ready API alternative and compare both approaches to assist you in determining which solution best fits your workflow.

Let’s get started!

Why Convert HTML to PDF Using Java?

In Java applications, PDF generation is often part of larger backend systems, billing platforms, reporting tools, enterprise dashboards, or document workflows. The question isn’t just how to generate a PDF, but how to generate one cleanly and consistently.

Using HTML as the source format makes that process more practical. Here is why you should convert HTML to PDF using Java:

Seamless Backend Integration: Java applications already work with structured data from databases, APIs, or microservices. HTML templates allow you to map that data into a structured layout before converting it into a PDF.

Better Layout Control with CSS: Instead of manually positioning elements in a PDF canvas, you can use CSS to control typography, spacing, tables, headers, and page breaks.

Maintainable and Scalable: Updating an HTML template is far easier than modifying low-level PDF generation logic. This becomes especially important in enterprise systems where document formats evolve.

Supports Production Workflows: Most Java HTML-to-PDF solutions rely on rendering engines that interpret HTML the way a browser would, making document formatting more predictable.

If you only need to convert a few files quickly without setting up a Java library, you can also use a free HTML to PDF converter.

HTML to PDF using Java Libraries

1. OpenPDF

OpenPDF is one of the most popular free and open-source Java libraries for creating and manipulating PDF documents.
It is a good choice for basic PDF generation and manipulation tasks. Here is a simple example demonstrating how to convert HTML to PDF using OpenPDF:

Step 1: Add OpenPDF Dependency

First, include OpenPDF in your project. If you are using Maven, add the following dependency to your pom.xml:

<dependency>
<groupId>com.github.librepdf</groupId>
<artifactId>openpdf</artifactId>
<version>1.3.26</version>
</dependency>

Step 2: Java Code to Convert HTML to PDF

Here is a basic Java code snippet that shows how to use OpenPDF to convert an HTML string to a PDF document:

import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.html.simpleparser.HTMLWorker;
import com.lowagie.text.pdf.PdfWriter;
import java.io.FileOutputStream;
import java.io.StringReader;
public class HtmlToPdfOpenPDF {
public static void main(String[] args) {
try {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream("HtmlToPdf.pdf"));
document.open();
String htmlString = "<html><body> This is my Project </body></html>";
HTMLWorker htmlWorker = new HTMLWorker(document);
htmlWorker.parse(new StringReader(htmlString));
document.close();
System.out.println("PDF created successfully.");
} catch (Exception e) {
e.printStackTrace();
}
}
}

This code snippet demonstrates a simple conversion of HTML content to a PDF document using OpenPDF. I have written a comprehensive guide on how to convert HTML to PDF using OpenPDF, do check that out.

2. Flying Saucer

Flying Saucer is a Java library that allows rendering XML or XHTML with CSS and converting it to PDF. Below is an example of how to use Flying Saucer for HTML to PDF conversion.

Step 1: Add Flying Saucer Dependency

Include Flying Saucer in your project. If you are using Maven, add the following dependencies to your pom.xml:

<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-core</artifactId>
<version>9.1.22</version>
</dependency>
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-pdf</artifactId>
<version>9.1.22</version>
</dependency>

Step 2: Java Code to Convert HTML to PDF

Here’s a basic Java code snippet demonstrating the use of Flying Saucer to convert HTML to a PDF document:

import org.xhtmlrenderer.pdf.ITextRenderer;
import com.lowagie.text.DocumentException;
import java.io.*;
public class HtmlToPdfFlyingSaucer {
public static void main(String[] args) {
String inputFile = "path/to/your/input.html";
String outputFile = "path/to/your/output.pdf";
try {
String url = new File(inputFile).toURI().toURL().toString();
OutputStream os = new FileOutputStream(outputFile);
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(url);
renderer.layout();
renderer.createPDF(os);
os.close();
System.out.println("PDF created successfully.");
} catch (DocumentException | IOException e) {
e.printStackTrace();
}
}
}

This code snippet shows how to use Flying Saucer with the ITextRenderer class to render an HTML file as a PDF document. For a more detailed guide on using Flying Saucer for HTML to PDF conversion, check out my comprehensive tutorial.

3. iText

iText is a popular Java library for creating and manipulating PDF documents. It provides extensive capabilities for HTML to PDF conversion. Here’s a basic example of how to use iText for this purpose.

Step 1: Add iText Dependency

Include iText in your project. If you are using Maven, add the following dependency to your pom.xml:

<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext7-core</artifactId>
<version>7.1.9</version>
</dependency>

Step 2: Java Code to Convert HTML to PDF

Below is a Java code snippet demonstrating the use of iText as a converter for HTML string to a PDF document:

import com.itextpdf.html2pdf.HtmlConverter;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import java.io.*;
public class HtmlToPdfIText {
public static void main(String[] args) {
String htmlContent = "<html><body><h1>Hello World</h1><p>This is a test of iText HTML to PDF conversion.</p></body></html>";
String pdfDest = "output.pdf";
try {
PdfDocument pdf = new PdfDocument(new PdfWriter(pdfDest));
HtmlConverter.convertToPdf(htmlContent, pdf);
System.out.println("PDF created successfully.");
} catch (IOException e) {
e.printStackTrace();
}
}
}

This example demonstrates converting a simple HTML string to PDF using iText’s HtmlConverter class.

4. Apache PDFBox

Apache PDFBox is a powerful open-source Java library used for handling PDF documents. It can be used to create new PDF documents, manipulate existing ones, and more. Here’s how you can use Apache PDFBox for HTML to PDF conversion.

Step 1: Add Apache PDFBox Dependency

Include Apache PDFBox in your project. If you are using Maven, add the following dependency to your pom.xml:

<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.24</version>
</dependency>

Step 2: Java Code to Convert HTML to PDF

Here’s a basic Java code snippet to demonstrate the conversion of HTML content to a PDF document using Apache PDFBox:

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import java.io.IOException;
public class HtmlToPdfPDFBox {
public static void main(String[] args) {
try (PDDocument document = new PDDocument()) {
PDPage page = new PDPage();
document.addPage(page);
PDPageContentStream contentStream = new PDPageContentStream(document, page);
contentStream.beginText();
contentStream.setFont(PDType1Font.HELVETICA, 12);
contentStream.newLineAtOffset(100, 700);
contentStream.showText("This is a sample text for PDFBox HTML to PDF Conversion.");
contentStream.endText();
contentStream.close();
document.save("PDFBoxExample.pdf");
System.out.println("PDF created successfully.");
} catch (IOException e) {
e.printStackTrace();
}
}
}

This example illustrates a simple use case of Apache PDFBox to create a PDF document with text content. Note that Apache PDFBox does not directly convert HTML to PDF but can be used to create PDFs programmatically. This example illustrates a simple use case of Apache PDFBox to create a PDF document with text content. Note that Apache PDFBox, often used in various document management software solutions, does not directly convert HTML to PDF but can be used to create PDFs programmatically.

Common HTML to PDF Conversion Challenges

1. CSS Compatibility

  • Different libraries support varying levels of CSS
  • Solutions for handling complex layouts
  • Best practices for responsive designs

2. Performance Optimization

  • Memory management techniques
  • Batch processing strategies
  • Caching mechanisms

3. Enterprise Considerations

  • Scalability factors
  • Security considerations
  • Compliance requirements

Comparing the Libraries

When selecting a Java library for HTML to PDF conversion, various factors such as features, ease of use, and specific project requirements must be considered.
Here’s a table comparing the libraries in 2024:

LibraryProsConsBest ForPerformanceMemory UsageCSS SupportJavaScript SupportUnicode SupportPriceActive Development
OpenPDFFree and open-source. Good for basic PDF generation and manipulation.Limited advanced features. May struggle with complex HTML and CSS.Simple PDF creation tasks where complex HTML rendering is not required.MediumLowBasicNoYesFreeActive
Flying SaucerGood support for CSS and XHTML. Integrates well with other PDF libraries.Does not support JavaScript. Limited to XHTML conversion.Projects requiring accurate rendering of CSS-styled HTML.GoodMediumGoodNoYesFreeModerate
iTextExtensive features for PDF manipulation. Supports HTML, CSS, JavaScript.Commercial license required for commercial use. Relatively complex API.Robust, enterprise-level applications requiring comprehensive PDF functionalities.ExcellentMediumExcellentYesYesCommercialVery Active
Apache PDFBoxPowerful for general PDF operations. Open-source. Supports text extraction, creation, and manipulation.Does not directly convert HTML to PDF. More suited for lower-level PDF operations.Custom PDF creation where direct HTML to PDF conversion is not the primary requirement.GoodLowManualNoYesFreeActive

Generate Dynamic PDFs in Bulk Using Templated (a PDF Generation API)

From your dashboard, you can create a new template from scratch or customize one from the Template Gallery, which has 100+ free templates that you can pick and adapt to your specific use cases.

Apart from this, you can also upload a PDF document, import a design from Canva or generate a template using AI.

Once selected, you’ll land in the visual drag and drop editor where you can modify text, layers, and layout elements.

For example, I’ve selected the Certificate of Achievement template.

From here, I can customize the design and define the specific layers, such as name, date, or title, that I want to dynamically update for different recipients.

Create PDFs with templates

Using Templated you can design your PDF template using a drag-and-drop editor and then generate your PDFs with an API.
Check the video below to see an example of the editor:

After logging in you will see your Dashboard where you can manage your templates or create new ones:

How to create a template from the dashboard

From your dashboard, you can create a new template from scratch or customize one from the Template Gallery, which has 100+ free templates that you can pick and adapt to your specific use cases.

Shows the certicate template in the Template Editor

To start using Templated API, you need to get your API key that can be found on the API Integration tab on your dashboard.

Shows where to get the API key from the Dashboard

Now that you have your Templated account ready, let’s see how you can integrate your application with the API. In this example we will be using a certificate template to generate PDFs.

import java.net.HttpURLConnection;
import java.net.URL;
import java.io.OutputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.json.JSONObject;
public class TemplatedApiRequest {
public static void main(String[] args) {
try {
URL url = new URL("https://api.templated.io/v1/render");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Authorization", "Bearer ${YOUR_API_KEY}");
conn.setDoOutput(true);
JSONObject payload = new JSONObject();
payload.put("template", "template_id");
payload.put("format", "pdf");
JSONObject layers = new JSONObject();
// Add layers to the changes object
changes.put("date", "02/10/2024");
changes.put("name", "John Doe");
changes.put("signature", "Dr. Mark Brown");
changes.put("details", "This certificate is awarded to John Doe in recognition of their successful completion of Computer Science Degree on 02/10/2024.");
payload.put("changes", changes);
// Add layers to the changes object
JSONObject dateLayer = new JSONObject();
dateLayer.put("text", "02/10/2024");
layers.put("date", dateLayer);
JSONObject nameLayer = new JSONObject();
nameLayer.put("text", "John Doe");
layers.put("name", nameLayer);
JSONObject signatureLayer = new JSONObject();
signatureLayer.put("text", "Dr. Mark Brown");
layers.put("signature", signatureLayer);
JSONObject detailsLayer = new JSONObject();
detailsLayer.put("text", "This certificate is awarded to John Doe in recognition of their successful completion of Computer Science Degree on 02/10/2024.");
layers.put("details", detailsLayer);
payload.put("layers", layers);
OutputStream os = conn.getOutputStream();
os.write(payload.toString().getBytes());
os.flush();
os.close();
int responseCode = conn.getResponseCode();
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// Print the response
System.out.println(response.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}

and If we check response we have the following

{
"renderUrl":"PDF_URL",
"status":"success",
"template":"YOUR_TEMPLATE_ID"
}

In the code above, using Templated to convert HTML to PDF is pretty simple. No additional libraries need to be installed. You only need to make a single API call, providing your data as the request body. That’s all there is to it!

You can use the renderUrl from the response to download or distribute the generated PDF.

HTML to PDF in Java vs Templated API

FeatureHTML to PDF with Java LibrariesTemplated API
SetupRequires library installation and rendering engine configurationNo setup required
InfrastructureYou manage servers, memory, and scalingFully managed infrastructure
Template CreationManual HTML coding requiredVisual editor, pre-built templates, AI generation
Layout UpdatesCode changes and redeployment are neededEdit directly in the dashboard
Bulk GenerationRequires backend optimization and scaling logicBuilt for high-volume generation
MaintenanceOngoing dependency updates and debuggingZero maintenance
AI Template CreationNot availableYes
Time to ProductionModerate to highVery fast

Other languages

You you want to learn how to convert HTML to PDF in other languages here are other resources for you to explore:

Conclusion

Converting HTML to PDF with Java offers flexibility and control, especially for backend-driven document workflows. Java libraries work well, but they require setup, dependency management, and scaling considerations.

For small projects, that’s manageable. For production systems or bulk generation, the operational overhead can grow quickly.

A dedicated PDF generation API like Templated simplifies the process. You can design templates visually, generate layouts using AI, and automate PDF generation through a simple API without handling infrastructure or rendering engines.

If you’d like to try it, sign up for Templated and get 50 free credits to start generating PDFs immediately. No credit card required.

Automate your images, videos and PDFs with a powerful API

Automate your marketing, social media visuals, banners, videos, PDFs and more with our
 API and no-code integrations

Learn More
Back to Blog

Ready to automate your images and PDFs?

Sign up to our free trial and try it for yourself

See our latest posts

View all posts »
Build vs Third-Party API: Things You Need to Know Before Adding Image Automation to Your Stack

Build vs Third-Party API: Things You Need to Know Before Adding Image Automation to Your Stack

Choosing between building an in-house image automation system and using a third-party API isn’t just about speed, it’s about scale, maintenance, and long-term cost. Here’s a practical breakdown to help you decide what actually works for your situation.

FFmpeg in Python: A Practical Guide with Code Examples

FFmpeg in Python: A Practical Guide with Code Examples

Learn how to use FFmpeg in Python with the ffmpeg-python library. Covers format conversion, audio extraction, video trimming, frame extraction, thumbnails, and a simpler API-based alternative with Templated.

3 Quick Ways To Generate Templates for Your Automation

3 Quick Ways To Generate Templates for Your Automation

Create automation templates faster using three smart methods: import Canva designs, generate layouts with Templated’s AI Template Generator, or leverage MCP integration for AI-powered creation.

4 Reasons to Switch from APITemplate for Image Automation

4 Reasons to Switch from APITemplate for Image Automation

Templated offers a powerful alternative to APITemplate for image automation. From importing Canva templates and AI-generated designs to a flexible visual editor and better developer support, it helps teams automate image creation faster and more easily.