I am working on a Grails project that uses the Flying Saucer library (via the Rendering plugin) to generate PDFs. I need those PDFs, which can have arbitrary lengths, to be forced to have an even page count. In other words: if the page count comes out odd, I need an empty page to be added to the end.
How can I achieve this? According to the user guide it should honour the normal CSS pagination rules, but that doesn't seem to work. I have tried rules like:
body {
page-break-after: right;
}
But they just seem to be ignored. I have tried variations such as html instead of body, left instead of right and even always.
Is this something this library can do? Perhaps there is some way to more directly tell it to create an even number of pages, not through CSS?
I ran into the same issue and was unable to solve it just with CSS (I think that @CBroe's comment is spot on).
The solution that ended up working for me was pretty hacky: I always add an extra blank page at the end of the document (by adding
<div style="page-break-before: always;"> </div>as the last element in thebody), and then cut off that extra page again if the rendered document ends up having an odd number of pages.I do the "cutting" by attaching a
PDFCreationListenerthat trims the renderer's internal page array to an even size (code is in Kotlin, but can be easily adjusted for other languages):I the above code,
rendereris theITextRenderer, which means that this code is specifically for theflying-saucer-pdf-itext5renderer. I'm not sure how this would need to be adjusted for the PDFBox-based renderer.