Java-based HTML to PDF Conversion Using Flying Saucer

Nov 24, 2025 · Programming · 11 views · 7.8

Keywords: Java | HTML to PDF | Flying Saucer | XHTML Rendering | Table Layout

Abstract: This technical paper provides an in-depth analysis of converting HTML/XHTML documents to PDF files within Java environments. It focuses on the core principles, configuration methods, and practical applications of the Flying Saucer renderer, supported by comprehensive code examples demonstrating high-quality PDF generation. The paper also compares alternative solutions like iText and WKHTMLTOPDF, offering developers thorough technical selection guidance. Key technical details such as table layout processing and CSS style support are thoroughly examined in real-world contexts.

Technical Background and Requirements Analysis

In modern enterprise application development, automated PDF report generation is a common requirement. Particularly within Java-based technology stacks, efficiently and accurately converting HTML/XHTML documents to PDF format while maintaining proper layout and style presentation poses significant challenges for developers. This paper provides a detailed analysis of several mainstream solutions based on practical project experience.

Flying Saucer Core Architecture Analysis

Flying Saucer is a Java-based XHTML renderer specifically designed for converting standards-compliant XHTML documents to PDF format. Its core architecture is built upon the CSS Box Model, parsing XHTML document structures, applying CSS style rules, and ultimately generating precise page layouts.

The project's key technical advantage lies in its excellent support for web standards. Flying Saucer effectively handles complex scenarios including table layouts, text flow, and image embedding, avoiding common issues like text overflow and layout misalignment found in traditional solutions. Its performance is particularly outstanding when processing table-intensive documents.

Environment Configuration and Dependency Management

To use Flying Saucer in a Java project, relevant dependencies must first be added to the build configuration. For Maven projects, the following dependencies should be configured in the pom.xml file:

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

These dependency packages provide complete XHTML parsing, CSS rendering, and PDF generation functionality. It's important to note that Flying Saucer has high requirements for XHTML standardization—input documents must comply with XML standards, including proper tag closure and attribute quoting.

Core Implementation Code Detailed Explanation

The following complete example demonstrates the basic usage of Flying Saucer, showing how to convert an XHTML document containing table layouts to a PDF file:

import org.xhtmlrenderer.pdf.ITextRenderer;
import java.io.*;

public class HtmlToPdfConverter {
    public void convertHtmlToPdf(String htmlContent, String outputPath) {
        try (OutputStream os = new FileOutputStream(outputPath)) {
            ITextRenderer renderer = new ITextRenderer();
            
            // Set document content
            renderer.setDocumentFromString(htmlContent);
            
            // Perform layout calculation
            renderer.layout();
            
            // Generate PDF document
            renderer.createPDF(os);
            
        } catch (Exception e) {
            throw new RuntimeException("PDF generation failed", e);
        }
    }
    
    // Usage example
    public static void main(String[] args) {
        String sampleHtml = "<html><body><table border='1'><tr><td>Cell 1</td><td>Cell 2</td></tr></table></body></html>";
        new HtmlToPdfConverter().convertHtmlToPdf(sampleHtml, "output.pdf");
    }
}

In this example, ITextRenderer is the core renderer class responsible for handling the entire process of document parsing, style application, and PDF generation. The setDocumentFromString() method accepts XHTML string input, the layout() method performs page layout calculation, and finally the createPDF() method outputs the final PDF document.

Table Layout Processing Technology

Addressing the table layout issues mentioned in the Q&A data, Flying Saucer provides specialized solutions. Its table rendering engine, implemented based on CSS 2.1 specifications, effectively handles complex scenarios including table width, cell merging, and border styles.

In practical applications, the following best practices are recommended to optimize table rendering effects:

// CSS example for optimizing table styles
String optimizedCss = """
table {
    border-collapse: collapse;
    width: 100%;
}
td, th {
    border: 1px solid #ddd;
    padding: 8px;
    text-align: left;
}
th {
    background-color: #f2f2f2;
}
""";

// Apply CSS styles to document
String styledHtml = "<style>" + optimizedCss + "</style>" + htmlContent;

Alternative Solutions Technical Comparison

Besides Flying Saucer, several other mainstream HTML to PDF solutions exist in the market, each with its applicable scenarios and technical characteristics.

iText Solution: iText is a mature Java PDF processing library whose HTMLWorker class provides basic HTML to PDF conversion functionality. However, it's important to note that iText's support for CSS and modern HTML features is relatively limited, potentially encountering compatibility issues when handling complex layouts.

// Basic usage example of iText
Document doc = new Document(PageSize.A4);
PdfWriter.getInstance(doc, out);
doc.open();
HTMLWorker hw = new HTMLWorker(doc);
hw.parse(new StringReader(html));
doc.close();

WKHTMLTOPDF Solution: A command-line tool based on the WebKit engine, offering excellent CSS and JavaScript support. Although not a Java library itself, it can be integrated through Java's Runtime.exec() or ProcessBuilder.

// Calling WKHTMLTOPDF through Java
ProcessBuilder pb = new ProcessBuilder("wkhtmltopdf", "input.html", "output.pdf");
Process process = pb.start();
int exitCode = process.waitFor();

Performance Optimization and Best Practices

In production environments, PDF generation performance is a critical consideration. The following are key optimization strategies:

Resource Caching: For frequently used template documents, pre-compile and cache renderer instances to avoid repeated parsing and layout calculation overhead.

Memory Management: When processing large documents, pay attention to timely release of memory resources occupied during rendering to prevent memory leaks.

Error Handling: Comprehensive exception handling mechanisms are crucial for production systems, requiring capture and proper handling of various possible conversion failure scenarios.

Application Scenarios and Extensions

HTML to PDF technology based on Flying Saucer is widely used in various enterprise-level scenarios, including:

Report Generation: Converting database query results into uniformly formatted PDF reports through HTML templates.

Document Archiving: Persistently saving dynamically generated web content as PDF format for long-term storage and reference.

Print Optimization: Providing specialized print views for web applications, ensuring print output quality and consistency through PDF format.

Through appropriate technology selection and optimization configuration, developers can build efficient and stable HTML to PDF conversion systems to meet various complex business requirements.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.