Thymeleaf Spring Boot Example
By AmarSivas | | Updated : 2021-03-16 | Viewed : 8401 times

A Quick tutorial explains pdf generation with
Table of Contents:
A Brief Intro For Thymeleaf
Maven dependencies
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>${thymeleaf.version}</version>
</dependency>
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-pdf</artifactId>
<version>${flying-saucer-pdf.version}</version>
</dependency>
Thymeleaf Example for PDF Generation
Setup for ClassLoaderTemplateResolver of Thymeleaf
Please see the below method in
public static String parseThymeleafTemplate(Map contentMap) {
ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode(TemplateMode.HTML);
TemplateEngine templateEngine = new TemplateEngine();
templateEngine.setTemplateResolver(templateResolver);
Context context = new Context();
context.setVariable("schoolName", contentMap.get("schoolName"));
context.setVariable("studentName", contentMap.get("studentName"));
context.setVariable("className", contentMap.get("className"));
context.setVariable("markLists", contentMap.get("markLists"));
return templateEngine.process("progress_report_template.html", context);
}
If you observe above code there are three important classes of Thymeleaf library.
-
1. ClassLoaderTemplateResolver
-
2. TemplateEngine
-
3. Context
The
Once done with settings into
The context is the main class where data objects passed through it. So whatever data object you pass to Context class then
Create HTML Template for Thymeleaf Parsing
Notice we create the
<table class="table table-stripped" style="color: white;background-color: #4da5a9;width:100%;">
<thead >
<tr>
<td>First Language</td>
<td>Second Language</td>
<td>Mathematics</td>
<td>General Science</td>
<td>Social Studies</td>
</tr>
</thead>
<tr th:each="marks:${markLists}">
<td th:text="${marks.firstLang}">Id</td>
<td th:text="${marks.secondLang}">Name</td>
<td th:text="${marks.mathematics}">Department</td>
<td th:text="${marks.generalScience}">Updated By</td>
<td th:text="${marks.socialStudies}">Updated On</td>
</tr>
</table>
If you notice the
And we used
Generate PDF using flying-saucer-pdf
Once the parsed the dynamic html content then it should be used to generate the pdf. Please look at the below method.
public static void generatePdfFromHtml(String pdfDirectory, String html) throws IOException, DocumentException {
String outputFolder = pdfDirectory + File.separator + "Progress-Report.pdf";
OutputStream outputStream = new FileOutputStream(outputFolder);
ITextRenderer renderer = new ITextRenderer();
renderer.setDocumentFromString(html);
renderer.layout();
renderer.createPDF(outputStream);
outputStream.close();
}
Here the parsed XHTML string will be processed for generating the pdf file in the required directory.
Please look at this repository Spring-Boot-PDF-Thymeleaf-Example-App for this example.