· tutorials · 5 min read

Updated on

How to Convert HTML to PDF using Flying Saucer

Step into the world of Java-based HTML to PDF conversions with Flying Saucer, complete with easy-to-follow code examples.

Summarize with AI:
Step into the world of Java-based HTML to PDF conversions with Flying Saucer, complete with easy-to-follow code examples.

Introduction

Converting HTML to PDF in Java sounds easy. Just render the page and export it, right?

Not quite.

If you’ve worked with PDFs before, you already know the drill. CSS behaves differently. Fonts disappear. Page breaks ruin your layout. And suddenly, a “simple export” becomes a debugging session. No one wants to do it, so many turn to the Flying Saucer to avoid it.

Flying Saucer is one of the most commonly used Java libraries for rendering HTML into PDF. It’s powerful, stable, and widely adopted. But like any rendering engine, it comes with its own setup steps and limitations.

In this guide, you will walk through exactly that. Everything from how to convert HTML to PDF using the Java Library Flying Saucer to the limitations and a better option for automating the generation of dynamic PDFs.

Sounds good? Read further.

Why Flying Saucer?

  • CSS Support: Exceptional support for CSS 2.1, enabling sophisticated document styling.
  • Ease of Use: Simple API for generating PDFs from HTML content.
  • Integration: Seamless integration with Java applications and libraries.

Getting Started with Flying Saucer

Step 1: Add Flying Saucer Dependencies

To utilize Flying Saucer, you need to include its core library along with a PDF renderer, such as iText, in your project. For Maven users, 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: Convert HTML to PDF with Flying Saucer

Flying Saucer requires well-formed XHTML to produce PDFs. If your HTML is not XHTML-compliant, you might need to preprocess it to conform to XHTML standards.

Here’s how you can convert XHTML to PDF using Flying Saucer:

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

In this example, ITextRenderer is used to render the XHTML content as a PDF. The setDocument method loads the XHTML file, layout prepares the content for rendering, and createPDF generates the PDF.

Enhancing PDFs with CSS

Flying Saucer’s strength lies in its CSS rendering capabilities. To leverage CSS for styling your PDF, include a <link> to your CSS file within the <head> of your XHTML document, just like in a regular HTML file.

If you only need to convert a few files quickly, you can also use our free HTML to PDF converter tool. It works best for small, one-off conversions without any setup.

A Better Approach: Template-Based PDF Generation with Templated

The examples above show how to convert HTML to PDF using Java libraries like Flying Saucer. That works.

But most dynamic automated PDF generation needs:

  • Structured templates for all PDF automation use cases (invoices, certificates, reports)
  • Dynamic data injection
  • Bulk generation
  • Reliable scaling
  • Minimal maintenance

That’s why a template-based PDF generation approach becomes particularly practical in this context.

Templated is a dedicated solution that offers a PDF generation API based on templates.

It offers multiple ways to create PDF templates:

Moreover, you can automate PDF generation in a way that fits your workflow.

Whether you’re a developer or part of a no-code team, Templated supports multiple integration options:

  • Spreadsheet-based bulk generation
  • No-code workflows
  • Java and other programming language integrations
  • MCP support

This flexibility allows you to reliably generate hundreds or even thousands of PDFs in just minutes, all while maintaining the integrity of your designs.

To get started, you’ll need access to the Templated API.

Sign up for free to receive 50 credits for testing. Once registered, you’ll land on your dashboard, where you can access your unique API key.

Shows where to get the API key from the Dashboard

Next, explore how the editor works and watch the video below to understand how template design and dynamic layers function for automation.

After creating your template in the editor, use the template ID along with your API key to automate generation. You can integrate it through code, connect it with no-code tools, or use spreadsheets for bulk workflows.

To go deeper, check out these helpful resources:

More ways to convert HTML to PDF

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

Conclusion

Flying Saucer remains a solid choice if you need to convert HTML to PDF within a Java environment. It gives you control over rendering and works well for structured layouts when configured properly.

But as soon as your requirements expand to dynamic templates, bulk generation, scaling, or ongoing maintenance, managing rendering engines and infrastructure can start to feel heavier than it should.

If your goal is to automate document workflows rather than just convert HTML files, using the reliable PDF generation APIs approach is often more efficient. With Templated, you design once, inject dynamic data, and generate PDFs at scale through a simple API without worrying about rendering setup or system overhead.

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

Share:
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 »
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.

Top 5 PDF Generator API Alternatives for HTML to PDF in 2026

Top 5 PDF Generator API Alternatives for HTML to PDF in 2026

Explore the top 5 PDF Generator API alternatives for 2026 to convert HTML to PDF seamlessly. Compare pricing, performance, and features to choose the best tool for automated PDF creation and document generation workflows.

How to Automate Employee Offer Letters with Templated

How to Automate Employee Offer Letters with Templated

Learn how to automate employee offer letters using Templated. Discover how HR teams can streamline document creation, personalize templates, and save time with automated workflows for faster and error-free onboarding.