How to Generate PDFs with Thymeleaf and iText in Spring Boot – A Step-by-Step Guide
This tutorial shows how to add iText and XHTMLRenderer dependencies, create a PDF utility class that supports Chinese fonts, configure Maven resources to avoid placeholder conflicts, and resolve Docker build issues, all illustrated with a working demo image.
Adding Maven Dependencies
<!--PDF转化工具-->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.4.2</version>
</dependency>
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>core-renderer</artifactId>
<version>R8</version>
</dependency>PDF Utility Class
import com.itextpdf.text.pdf.BaseFont;
import com.lowagie.text.DocumentException;
import org.springframework.stereotype.Component;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;
import java.io.IOException;
import java.io.OutputStream;
/**
* Export PDF file conversion tool
*/
public class PDFUtil {
/**
* Generate PDF file
* @param out output stream
* @param html HTML string
* @throws IOException IO exception
* @throws DocumentException Document exception
*/
public static void createPDF(OutputStream out, String html) throws IOException, DocumentException {
ITextRenderer renderer = new ITextRenderer();
renderer.setDocumentFromString(html);
// Resolve Chinese font support
ITextFontResolver fontResolver = renderer.getFontResolver();
fontResolver.addFont("pdf/font/fangsong.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
fontResolver.addFont("pdf/font/PingFangSC.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
renderer.layout();
renderer.createPDF(out);
}
}Font files must be downloaded and placed under the resources directory; adjust the paths in the code accordingly.
Demo Output
Common Issues and Fixes
1. Maven packaging alters HTML placeholders
Cause: Maven filtering processes ${} placeholders, which conflicts with Thymeleaf syntax.
Solution: Configure the section in pom.xml to disable filtering for HTML files.
<build>
...
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>**/*.html</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<includes>
<include>**/*.html</include>
</includes>
</resource>
</resources>
</build>2. Docker build fails due to missing font classes
Use the java:8 base image instead of openjdk:8-jre-alpine to ensure required font handling classes are available.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
