Skip to content

Commit 796144d

Browse files
author
Samuel Huylebroeck
committed
Guard against NPE when converting to Elements
Guard when using OutlineHandler Add test DEVSIX-1679
1 parent f942189 commit 796144d

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/main/java/com/itextpdf/html2pdf/attach/impl/OutlineHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ public void reset() {
178178
*/
179179
OutlineHandler addOutline(ITagWorker tagWorker, IElementNode element, ProcessorContext context) {
180180
String tagName = element.name();
181-
if (null != tagWorker && hasTagPriorityMapping(tagName)) {
181+
if (null != tagWorker && hasTagPriorityMapping(tagName) && context.getPdfDocument() != null) {
182182
int level = (int) getTagPriorityMapping(tagName);
183183
if (null == currentOutline) {
184184
currentOutline = context.getPdfDocument().getOutlines(false);
@@ -213,7 +213,7 @@ OutlineHandler addOutline(ITagWorker tagWorker, IElementNode element, ProcessorC
213213
*/
214214
OutlineHandler addDestination(ITagWorker tagWorker, IElementNode element) {
215215
String tagName = element.name();
216-
if (null != tagWorker && hasTagPriorityMapping(tagName)) {
216+
if (null != tagWorker && hasTagPriorityMapping(tagName) && destinationsInProcess.size()>0) {
217217
String content = destinationsInProcess.pop();
218218
if (tagWorker.getElementResult() instanceof IElement) {
219219
tagWorker.getElementResult().setProperty(Property.DESTINATION, content);

src/test/java/com/itextpdf/html2pdf/Html2ElementsTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ This file is part of the iText (R) project.
4242
*/
4343
package com.itextpdf.html2pdf;
4444

45+
import com.itextpdf.html2pdf.attach.impl.OutlineHandler;
4546
import com.itextpdf.layout.element.IElement;
4647
import com.itextpdf.layout.element.Paragraph;
4748
import com.itextpdf.layout.element.Table;
@@ -152,4 +153,24 @@ public void htmlToElementsTest08() throws IOException {
152153
String html = "<html><p>Hello world!</p><meta name=\"author\" content=\"Bruno\"><table><tr><td>123</td><td><456></td></tr><tr><td>Long cell</td></tr></table><p>Hello world!</p></html>";
153154
HtmlConverter.convertToElements(html);
154155
}
156+
157+
@Test
158+
//Test OutlineHandler exception throwing
159+
public void htmlToElementsTest09() throws IOException {
160+
/*
161+
Outlines require a PdfDocument, and OutlineHandler is based around its availability
162+
Any convert to elements workflow of course doesn't have a PdfDocument.
163+
Instead of throwing an NPE when trying it, the OutlineHandler will check for the existence of a pdfDocument
164+
If no PdfDocument is found, the handler will do nothing silently
165+
*/
166+
String html = "<html><p>Hello world!</p><meta name=\"author\" content=\"Bruno\"><table><tr><td>123</td><td><456></td></tr><tr><td>Long cell</td></tr></table><p>Hello world!</p></html>";
167+
ConverterProperties props = new ConverterProperties();
168+
OutlineHandler outlineHandler = new OutlineHandler();
169+
outlineHandler.putTagPriorityMapping("h1", 1);
170+
outlineHandler.putTagPriorityMapping("h3",2);
171+
outlineHandler.putTagPriorityMapping("p", 3);
172+
props.setOutlineHandler(outlineHandler);
173+
174+
HtmlConverter.convertToElements(html);
175+
}
155176
}

0 commit comments

Comments
 (0)