Creating PDFs Files Using Thymeleaf

Creating PDFs can by a difficult undertaking. On the off chance that you ever worked with templating motors like Jasper you presumably realize what I mean. On the opposite side, HTML makes it incredibly easy to depict archives. So why not use HTML to create PDFs?

The accompanying post shows a basic way to deal with create PDFs with Thymeleaf formats and Flying Saucer in plain Java.

With highlights like common templating and complex handling, Thymeleaf can have a necessary influence of machine learning-based predictive analytic services.. The advantage of smoothing out front-end and back-end designers utilizing Thymeleaf is another ground-breaking highlight for machine learning development.

Our PDF delivering pipeline comprises of two fundamental advances: Utilizing Thymeleaf, we populate XHTML formats with information so as to get plain XHTML archive.

We spare this XHTML report as a PDF utilizing Flying Saucer.

Note that the two stages are free. You could without much of a stretch utilize some other templating motor (similarly as Apache FreeMarker) or even plain XHTML. Nonetheless, I would lean toward Thymeleaf as it is an experienced templating motor particularly on the off chance that you work in a Spring situation.

Task Setup

To begin delivering, we need three distinct conditions which we can discover in Maven:

Thymeleaf Template

The Thymeleaf format which we are utilizing is truly basic. It just contains a sentence and variable to load up with information. Note this is an exceptionally basic model. Thymeleaf is substantially more remarkable and gives ideas like to circles, if-explanations and Java strategy gets out of formats. You will discover a great deal of instructional exercises on the web.

To deliver this layout with Thymeleaf, we can utilize the accompanying code. It will substitute the variable ${name} with real information. At long last, we will get plain HTML which we could open in any program.

PDF Generation

Creating the genuine PDF is very simple at this point. We essentially put the HTML string to the PDF renderer which restores the byte stream of the created PDF. The essential format fits for our model, yet we could likewise remember some CSS for request to style the PDF.

Progressed Topics

A great deal of significant themes are not canvassed in the extremely essential model above. Be that as it may, I need to give some outside connections and clues on a few focuses: You can utilize CSS so as to produce page numbers with Flying Saucer as depicted here.Thymeleaf formats can be separated into parts. Those parts can be incorporated into different layouts and gave reusable structure squares to records, (for example, a header or footer). Peruse more here.

Learn more: Generating Dynamic PDFs Using Thymeleaf


0 Comments

Curated for You

Popular

Top Contributors more

Latest blog