#Making smart pagebreaks in Thymeleaf

1 messages · Page 1 of 1 (latest)

scenic dune
#

I have some code that takes some data and fills out a Thymeleaf template that then Flying Saucer turns into a pdf and it's beautiful. 🤌

The template is composed of many tables. Sometimes a table needs a variable number of rows, and the iterator can tell me that. Sometimes a table row has so wide data that the row needs another line, and that is much harder to predict (no fixed width font).

Thymeleaf is perfectly happy to chop a row in half when a page runs out of space, and put the other half on the next page. Not even at a linebreak; you can have the top of the characters on one page and the bottom on the next. This of course looks awful, so I have added some logic that roughly estimates space consumption based on table row count and makes page breaks accordingly. But, that doesn't work for wide data taking up extra lines.

The ideal solution would be that I could give certain divs an "atomic" class and make Thymeleaf act like so: If an atomic div cannot fit on the current page, make a pagebreak first. If a pagebreak was just made and an atomic div still cannot fit, do your thing and chop it off. Is there any way to accomplish this?

magic garnetBOT
#

<@&987246964494204979> please have a look, thanks.

#

Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#
TJ-Bot
I have some code that takes some data and fills out a Thymeleaf template that then Flying Saucer turns into a pdf and it's beautiful. 🤌 The template is composed of many tables. Sometimes a table needs a variable number of rows, and the iterator can tell

of multiple sections and I would like to make smart page breaks so that each section starts on a new page when it overflows to the next page.

One way to achieve this is by using Thymeleaf's conditional logic in your template. You can check the height of each section and if it exceeds a certain threshold, you can insert a page break using CSS.

For example, you can use Thymeleaf's th:if attribute to check the height of each section like this:

<div th:if="${sectionHeight > MAX_HEIGHT}" style="page-break-before: always;">
    <!-- Section content -->
</div>

In this example, MAX_HEIGHT is a predefined value that you can adjust based on the size of your pages. If the sectionHeight exceeds MAX_HEIGHT, a page break will be inserted before that section.

Another approach is to dynamically calculate the height of each section using JavaScript and then insert a page break if needed. You can do this by adding an event listener to detect when a section overflows its container and then insert a page break accordingly.

By implementing these strategies, you can create smart page breaks in your Thymeleaf template that ensure each section starts on a new page when necessary.

scenic dune
#

ok that gpt response sounds promising, will try it

#

ok, sectionHeight isn't something thymeleaf provides :/ that answer wasn't useful