openhtmltopdf icon indicating copy to clipboard operation
openhtmltopdf copied to clipboard

page-break-inside: avoid not working (maybe)

Open ralleman opened this issue 6 years ago • 8 comments

Hi I don't know if there's a bug or I'm misunderstand how to do it. See attached.

post-pdf.pdf template2.html.txt

I want to ensure that the entire region from "Special notes ..." and "Subtotal ..." down to "Total ..." is not broken across pages. How can I do that?

Also the "Special notes..." are overlapping the "final-messages" block. Is there a way to prevent this. This overlap doesn't happen in Chrome.

Richard

ralleman avatar Sep 14 '18 15:09 ralleman

Looks like the additional rowspan was causing the overlapping text with "special notes". However, I still haven't been successful in getting the entire summary table sitting on the same page. I think I need help with that.

ralleman avatar Sep 15 '18 01:09 ralleman

I am seeing the same issue, pretty heavy bug if you ask me...

dxshindeo avatar Dec 04 '19 10:12 dxshindeo

page-breaks-inside: avoid got worked on span/div or any tag with style="display: inline-block". .

sramcse avatar Aug 04 '20 12:08 sramcse

I also had issues with page-break-inside: avoid. The issue was that display: block was not set on the HTML-element.

I am using custom elements, e.g. <section>, <subsection>. These seem to have a different display-Value than a standard div-Element.

Anyway, now that I use page-break-inside: avoid together with display: block, everything works as expected.

achimmihca avatar Mar 16 '21 15:03 achimmihca

I came across a variant of this problem as well. For us, the problem surfaced because we got an internal server error from the Pod running our PDF generation (which is using OpenHTMLToPDF as the backbone) because apparently the program tried to stretch a BoundingBox intended for one page over two pages, which resulted in an IndexOutOfBoundException because in the resultset there was only one PageResult and not two.

It was also fixed by setting the display value to block, as @achimmihca suggested. Would still be great if there was some sort of more permanent fix for this, as many people would assume that page-break-inside: avoid works correctly no matter what the CSS display value of the element is.

mcser avatar Sep 22 '21 09:09 mcser

Hi @mcser,

Thanks for the info. It should work on all display values except display: inline which generally try to avoid page breaks inside anyway. So it should just ignore page-break-inside: avoid on inline boxes, rather than crash. Alas, the issues are piling up!

danfickle avatar Sep 26 '21 05:09 danfickle

Might be a different issue then. I will try to get a stacktrace and minimal working example later.

mcser avatar Sep 26 '21 07:09 mcser

Unfortunately I have not been able to reproduce the issue using a simple sample page. The only way I could reliably get the renderer to crash was using a very specific business case from my application which i can't fully disclose here for privacy reasons.

I do, however, have a stacktrace of the issue starting from the PdfRendererBuilder.run in hopes that it may help detect the issue.

java.lang.IndexOutOfBoundsException: Index 1 out of bounds for length 1 at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64) at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70) at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248) at java.base/java.util.Objects.checkIndex(Objects.java:372) at java.base/java.util.ArrayList.get(ArrayList.java:459) at com.openhtmltopdf.render.displaylist.PagedBoxCollector.getPageResult(PagedBoxCollector.java:927) at com.openhtmltopdf.render.displaylist.PagedBoxCollector.collectInline(PagedBoxCollector.java:288) at com.openhtmltopdf.render.displaylist.PagedBoxCollector.collect(PagedBoxCollector.java:271) at com.openhtmltopdf.render.displaylist.DisplayListCollector.collect(DisplayListCollector.java:150) at com.openhtmltopdf.render.displaylist.DisplayListCollector.collectLayers(DisplayListCollector.java:38) at com.openhtmltopdf.render.displaylist.DisplayListCollector.collect(DisplayListCollector.java:169) at com.openhtmltopdf.render.displaylist.DisplayListCollector.collectRoot(DisplayListCollector.java:88) at com.openhtmltopdf.pdfboxout.PdfBoxRenderer.writePDFFast(PdfBoxRenderer.java:610) at com.openhtmltopdf.pdfboxout.PdfBoxRenderer.createPdfFast(PdfBoxRenderer.java:557) at com.openhtmltopdf.pdfboxout.PdfBoxRenderer.createPDF(PdfBoxRenderer.java:483) at com.openhtmltopdf.pdfboxout.PdfBoxRenderer.createPDF(PdfBoxRenderer.java:420) at com.openhtmltopdf.pdfboxout.PdfBoxRenderer.createPDF(PdfBoxRenderer.java:402) at com.openhtmltopdf.pdfboxout.PdfRendererBuilder.run(PdfRendererBuilder.java:46)

The issue showed up during the rendering of a .peb template which could potentially span across 2-3 lines of text and from what i could gather, OpenHTMLToPdf tries to spread this element across two seperate pages but fails to do so. The quick-fix by achimmihca solved the issue as explained above.

The code piece that triggers the failure looks something like this

<div style="page-break-inside: avoid"> {% include fileService.getTemplateFile("template.peb") %} </div> I hope this helps resolve the issue.

mcser avatar Sep 28 '21 07:09 mcser