Tutorial for Generating Dynamic PDFs Using Thymeleaf

Posted By : Harshit Verma | 01-Sep-2020

Generating Dynamic PDFs Using Thymeleaf

 

 

Thymeleaf is a Java XML/XHTML/HTML5 layout that can work both in web (servlet-based) and non-web situations. It is more qualified for serving XHTML/HTML5 at the view layer of MVC-based web applications, yet it can deal with any XML document even in disconnected situations. In this article, we at Oodles share a comprehensive guide to generating PDFs using Thymeleaf and Springboot. 

 

With features like natural templating and complex processing, Thymeleaf can play an integral part of machine learning-based predictive analytic services. The benefit of streamlining front-end and back-end developers using Thymeleaf is another powerful feature for machine learning development.

 

Steps to Generate PDFs Using Thymeleaf

 

It's easy to create a template using the Thymeleaf. Firstly we need to add the dependency within our pom.xml file for the flying-saucer that will help to generate the PDF file.

 

Dependency are :

 

<dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf</artifactId>
    <version>3.0.11.RELEASE</version>
</dependency>

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

 

Now, after adding the dependencies we need to create a sample Thymeleaf template, which contains the dynamic fields that are used to provide values dynamically within the template.

 

<html xmlns:th="http://www.thymeleaf.org">
  <body>
    <h3 style="text-align: center; color: green">
      <span th:text="'Welcome to ' + ${to} + '!'"></span>
    </h3>
  </body>
</html>

 

After that we need to parse the thymeleaf template, that contains the dynamic data, passed by us.The template engine provides tha facility of custom configuration that will ease our work. Refer the below code for parsing thymleaf template.

 

private String parseThymeleafTemplate() {
    ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
    templateResolver.setSuffix(".html");
    templateResolver.setTemplateMode(TemplateMode.HTML);
 
    TemplateEngine templateEngine = new TemplateEngine();
    templateEngine.setTemplateResolver(templateResolver);
 
    Context context = new Context();
    context.setVariable("to", "Sample");
 
    return templateEngine.process("thymeleaf_template", context);
}

 

The above function parses the template and returns an HTML string that contains the dynamic data.

Now, we need to pass the generated string as an input to the function that creates the PDF. For that we need to provide the output location of the file, this is the desired location where we want to generate the result as PDF.


 

public void generatePdfFromHtml(String html) {
    String outputFolder = System.getProperty("user.home") + File.separator + "thymeleaf.pdf";

    OutputStream outputStream = new FileOutputStream(outputFolder);
    ITextRenderer renderer = new ITextRenderer();
    renderer.setDocumentFromString(html);
    renderer.layout();
    renderer.createPDF(outputStream);
 
    outputStream.close();
}

 

Conclusion

 

Creating a dynamic PDF using Thymleaf is quite easy. As should be obvious, the content is green and adjusted to the inside as characterized in our inline CSS. This is an amazingly integral asset for modifying our PDFs.

 

Request For Proposal

Sending message..

Ready to innovate ? Let's get in touch

Chat With Us