Converting HTML to PDF in Java Using Flying Saucer

·

3 min read

Converting HTML to PDF in Java Using Flying Saucer

In today's digital world, converting HTML files to PDF documents programmatically is a common requirement, especially in enterprise applications dealing with report generation, document archival, and more. In this blog post, we'll explore how to accomplish this task using Java with the help of Flying Saucer library along with Maven dependencies.

Introduction to Flying Saucer

Flying Saucer is an open-source Java library that provides an easy way to convert XHTML/XML documents to PDF. It utilizes the powerful iText library under the hood for PDF generation.

Setting Up Maven Dependencies

<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>
<dependency>
    <groupId>org.xhtmlrenderer</groupId>
    <artifactId>flying-saucer-pdf-openpdf</artifactId>
    <version>9.1.20</version>
</dependency>
<dependency>
    <groupId>org.jsoup</groupId>
    <artifactId>jsoup</artifactId>
    <version>1.13.1</version>
</dependency>

These dependencies include the core Flying Saucer library, PDF rendering functionality, Flying Saucer PDF implementation using OpenPDF, and Jsoup library for HTML parsing.

Converting HTML to PDF - Java Code

Now, let's dive into the Java code that performs the conversion:

import java.io.FileOutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.commons.io.FileUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.xhtmlrenderer.pdf.ITextRenderer;

public class PdfGenerator {

    private static String htmlToXhtml(String html) {
        // Convert HTML to XHTML
        Document document = Jsoup.parse(html);
        document.outputSettings().syntax(Document.OutputSettings.Syntax.xml);
        return document.html();
    }

    public static void main(String[] args) {
        String inputFile = "appointment_letter.html"; // Path to your XHTML/XML file
        String outputFile = "output.pdf"; // Path to the output PDF file

        try {
            // Create an ITextRenderer instance
            ITextRenderer renderer = new ITextRenderer();

            // Read HTML content from file
            String content = FileUtils.readFileToString(Paths.get(inputFile).toFile(), StandardCharsets.UTF_8);

            // Perform replacements
            Map<String, String> valueMap = new HashMap<>();
            valueMap.put("employeeId", "20240200001");
            valueMap.put("employeeName", "Harish Jay Raj");
            valueMap.put("startDate", "25-03-2024");

            Set<Entry<String, String>> entrySet = valueMap.entrySet();
            for (Entry<String, String> es : entrySet) {
                content = content.replace("@{" + es.getKey() + "}", es.getValue());
            }

            // Convert HTML to XHTML
            String htmlToXhtml = PdfGenerator.htmlToXhtml(content);
            renderer.setDocumentFromString(htmlToXhtml);

            // Render the document to PDF
            renderer.layout();
            FileOutputStream fos = new FileOutputStream(outputFile);
            renderer.createPDF(fos);
            fos.close();

            System.out.println("PDF generated successfully!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Understanding the Code

  • We use ITextRenderer class from Flying Saucer to render HTML content to PDF.

  • The HTML content is read from a file (appointment_letter.html in this case) and stored as a string.

  • We perform dynamic content replacement using a Map to replace placeholders like @{employeeId} and @{employeeName} with actual values.

  • The HTML content is converted to XHTML using Jsoup library to ensure compatibility with Flying Saucer.Finally, the XHTML content is rendered to PDF and saved to the specified output file (output.pdf).

Sample appointment.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Appointment Letter</title>
    <style>
        body {
            font-family: Arial, sans-serif;
        }

        .letter {
            max-width: 600px;
            margin: 0 auto;
            padding: 20px;
            border: 1px solid #ccc;
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }

        .header {
            text-align: center;
            margin-bottom: 20px;
        }

        .employee-info {
            margin-bottom: 20px;
        }

        .closing {
            margin-top: 20px;
        }
    </style>
</head>
<body>
    <div class="letter">
        <div class="header">
            <h2>Appointment Letter</h2>
        </div>

        <p>
            Dear <span id="employeeName">@{employeeName}</span>,
        </p>

        <p>
            We are pleased to inform you that you have been appointed as an employee at XYZ Company. Your dedication and skills have earned you the position with the following details:
        </p>

        <div class="employee-info">
            <p><strong>Employee ID:</strong> <span id="employeeId">@{employeeId}</span></p>
        </div>

        <p>
            Your employment with XYZ Company will commence on <span id="startDate">@{startDate}</span>. Please report to the HR department on your joining date for further orientation and formalities.
        </p>

        <p>
            We believe that your expertise will contribute significantly to our team, and we look forward to your valuable contributions.
        </p>

        <div class="closing">
            <p>Best regards,</p>
            <p>The XYZ Company Team</p>
        </div>
    </div>
</body>
</html>

Below is generated PDF file screenshot.

Conclusion

In this tutorial, we explored how to convert HTML files to PDF documents using Java with Flying Saucer library. This approach offers flexibility and customization options for generating PDF documents from HTML content programmatically. By following the provided steps and code snippets, you can easily integrate HTML to PDF conversion functionality into your Java applications.