danfickle / openhtmltopdf

An HTML to PDF library for the JVM. Based on Flying Saucer and Apache PDF-BOX 2. With SVG image support. Now also with accessible PDF support (WCAG, Section 508, PDF/UA)!

Home Page:https://danfickle.github.io/pdf-templates/index.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

NoSuchElementException

fcorneli opened this issue · comments

On one of our templates, we could trigger the following exception:

java.util.NoSuchElementException
	at java.util.LinkedList.getLast(LinkedList.java:257)
	at com.openhtmltopdf.layout.LayoutContext.getBlockFormattingContext(LayoutContext.java:257)
	at com.openhtmltopdf.layout.LayoutContext.translate(LayoutContext.java:306)
	at com.openhtmltopdf.newtable.TableBox.calcPageClearance(TableBox.java:562)
	at com.openhtmltopdf.newtable.TableBox.layout(TableBox.java:227)
	at com.openhtmltopdf.layout.Layer.layoutAbsoluteChild(Layer.java:1027)
	at com.openhtmltopdf.layout.Layer.layoutAbsoluteChildren(Layer.java:949)
	at com.openhtmltopdf.layout.Layer.finish(Layer.java:931)
	at com.openhtmltopdf.layout.LayoutContext.popLayer(LayoutContext.java:292)
	at com.openhtmltopdf.render.BlockBox.layout(BlockBox.java:1107)
	at com.openhtmltopdf.render.BlockBox.layout(BlockBox.java:984)
	at com.openhtmltopdf.pdfboxout.PdfBoxRenderer.layout(PdfBoxRenderer.java:347)
	at com.openhtmltopdf.pdfboxout.PdfRendererBuilder.run(PdfRendererBuilder.java:45)

If I do something quick-and-dirty like:

diff --git a/openhtmltopdf-core/src/main/java/com/openhtmltopdf/layout/LayoutContext.java b/openhtmltopdf-core/src/main/java/com/openhtmltopdf/layout/LayoutContext.java
index 990f1c10..d2d4bc0d 100644
--- a/openhtmltopdf-core/src/main/java/com/openhtmltopdf/layout/LayoutContext.java
+++ b/openhtmltopdf-core/src/main/java/com/openhtmltopdf/layout/LayoutContext.java
@@ -51,6 +51,7 @@ import com.openhtmltopdf.render.FSFont;
 import com.openhtmltopdf.render.FSFontMetrics;
 import com.openhtmltopdf.render.MarkerData;
 import com.openhtmltopdf.render.PageBox;
+import java.util.NoSuchElementException;
 
 /**
  * This class tracks state which changes over the course of a layout run.
@@ -303,7 +304,11 @@ public class LayoutContext implements CssContext {
     }
 
     public void translate(int x, int y) {
-        getBlockFormattingContext().translate(x, y);
+        try {
+            getBlockFormattingContext().translate(x, y);
+        } catch (NoSuchElementException e) {
+
+        }
     }
 
     /* code to keep track of all of the id'd boxes */

it no longer fails of course, but I don't see my page footer anymore.

Hi @fcorneli,

It turns out that the layout for table boxes was not aware of stacking context layers or block formatting contexts (both required for position: absolute). I have now fixed. In the meantime, you can use a surrounding div with the position: absolute style rather than putting that style directly on the table.

Thanks for reporting.