Fix #1163: throw DocumentException instead of writing 'ERROR: Infinite table loop' into the PDF - #1596
Conversation
… Infinite table loop' into the PDF When a PdfPTable row can never fit on a page (e.g. a cell with a fixed height larger than the page height), the layout loop in PdfDocument.addPTable gave up after three attempts and silently added the literal text 'ERROR: Infinite table loop' to the generated document. Callers got a corrupted PDF and no signal that anything went wrong. Throw a DocumentException with a descriptive, localized message instead so the error surfaces through the normal Document.add error channel. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 0 |
| Duplication | 2 |
NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.
|
|
To add to this. I was just getting a pdf conformance error due to a non-embedded font (Helvetica, which is the default). After being baffled, and looking into the bug (took some time), I discovered that the problem was this "ERROR: Infinite table loop" text being inserted. This was causing the conformance error. When in truth, the actual problem was with an image not being scaled correctly and not fitting in a table 😵💫 Having an error for this would be much better, and save people some time (not being paranoid about their font usage) |



Summary
Fixes #1163.
When a
PdfPTablerow can never fit on a page — typically a cell with a fixed height larger than the page height, as in the issue's reproduction — the layout loop inPdfDocument.addPTablegave up after three attempts and silently wrote the literal textERROR: Infinite table loopinto the generated document. The caller received a corrupted PDF and no signal that anything went wrong; the reporter only discovered the text on the last page of the output.Fix
Throw a
DocumentExceptionwith a descriptive, localized message instead:This surfaces the error through the normal
Document.add(...)error channel (which already declaresDocumentException), so callers can catch it and fix their layout, rather than shipping a broken document.Note on behavior: code that previously "succeeded" while producing a PDF containing the error text will now get an exception. That is the intent — the old behavior corrupted the output silently.
Tests
New
TableRowTooTallTest:rowTallerThanPageThrowsInsteadOfWritingErrorText— a one-cell table withfixedHeight = pageHeight + 100now throwsDocumentException(previously: silently produced a PDF containing the error text). Guarded by a preemptive timeout so a regression to a genuine infinite loop fails fast.fittingTableIsUnaffected— normal tables still render, with no error text in the extracted output.Full
openpdf-coresuite passes (the existingTableEndlessTestscenarios are unaffected).