From 45f68e349e3f870fee5d4604fcc8115938484c52 Mon Sep 17 00:00:00 2001 From: Simon Steiner Date: Thu, 30 Mar 2023 07:45:01 +0100 Subject: FOP-3125: NPE when using broken link and PDF 1.5 --- .../apache/fop/render/pdf/PDFDocumentHandler.java | 1 + .../render/pdf/PDFDocumentNavigationHandler.java | 6 ++++ .../DocumentNavigationHandlerTestCase.java | 22 ++++++++++-- .../fop/render/extensions/linkmissingdest.if.xml | 42 ++++++++++++++++++++++ 4 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 fop-core/src/test/resources/org/apache/fop/render/extensions/linkmissingdest.if.xml (limited to 'fop-core/src') diff --git a/fop-core/src/main/java/org/apache/fop/render/pdf/PDFDocumentHandler.java b/fop-core/src/main/java/org/apache/fop/render/pdf/PDFDocumentHandler.java index ae2053061..09eb12c54 100644 --- a/fop-core/src/main/java/org/apache/fop/render/pdf/PDFDocumentHandler.java +++ b/fop-core/src/main/java/org/apache/fop/render/pdf/PDFDocumentHandler.java @@ -183,6 +183,7 @@ public class PDFDocumentHandler extends AbstractBinaryWritingIFDocumentHandler { /** {@inheritDoc} */ public void endDocument() throws IFException { + documentNavigationHandler.registerIncompleteActions(); pdfDoc.getResources().addFonts(pdfDoc, fontInfo); try { if (pdfDoc.isLinearizationEnabled()) { diff --git a/fop-core/src/main/java/org/apache/fop/render/pdf/PDFDocumentNavigationHandler.java b/fop-core/src/main/java/org/apache/fop/render/pdf/PDFDocumentNavigationHandler.java index 005c82a6f..552139980 100644 --- a/fop-core/src/main/java/org/apache/fop/render/pdf/PDFDocumentNavigationHandler.java +++ b/fop-core/src/main/java/org/apache/fop/render/pdf/PDFDocumentNavigationHandler.java @@ -29,6 +29,7 @@ import org.apache.fop.pdf.PDFDocument; import org.apache.fop.pdf.PDFFactory; import org.apache.fop.pdf.PDFGoTo; import org.apache.fop.pdf.PDFLink; +import org.apache.fop.pdf.PDFObject; import org.apache.fop.pdf.PDFOutline; import org.apache.fop.pdf.PDFReference; import org.apache.fop.pdf.PDFStructElem; @@ -214,4 +215,9 @@ public class PDFDocumentNavigationHandler implements IFDocumentNavigationHandler return action.getID(); } + public void registerIncompleteActions() { + for (Object action : incompleteActions.values()) { + getPDFDoc().addObject((PDFObject) action); + } + } } diff --git a/fop-core/src/test/java/org/apache/fop/render/extensions/DocumentNavigationHandlerTestCase.java b/fop-core/src/test/java/org/apache/fop/render/extensions/DocumentNavigationHandlerTestCase.java index 1b8efd112..0578d27e7 100644 --- a/fop-core/src/test/java/org/apache/fop/render/extensions/DocumentNavigationHandlerTestCase.java +++ b/fop-core/src/test/java/org/apache/fop/render/extensions/DocumentNavigationHandlerTestCase.java @@ -109,13 +109,14 @@ public class DocumentNavigationHandlerTestCase { @Test public void testGotoXYMergedIF() throws Exception { InputStream ifXml = getClass().getResourceAsStream("link.if.xml"); - ByteArrayOutputStream pdf = iFToPDF(ifXml); + ByteArrayOutputStream pdf = iFToPDF(ifXml, ""); Assert.assertTrue(pdf.toString().contains("/S /GoTo")); } - private ByteArrayOutputStream iFToPDF(InputStream is) throws Exception { + private ByteArrayOutputStream iFToPDF(InputStream is, String fopxconf) throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); - FOUserAgent userAgent = FopFactory.newInstance(new File(".").toURI()).newFOUserAgent(); + FOUserAgent userAgent = FopFactory.newInstance(new File(".").toURI(), + new ByteArrayInputStream(fopxconf.getBytes())).newFOUserAgent(); Transformer transformer = TransformerFactory.newInstance().newTransformer(); Source src = new StreamSource(is); IFDocumentHandler documentHandler @@ -255,4 +256,19 @@ public class DocumentNavigationHandlerTestCase { GoToXYAction a = (GoToXYAction) b.getAction(); Assert.assertEquals(a.getPageIndex(), 0); } + + @Test + public void testGotoXYMissingDestIF() throws Exception { + InputStream ifXml = getClass().getResourceAsStream("linkmissingdest.if.xml"); + String fopxconf = "\n" + + " true\n" + + " \n" + + " \n" + + " 1.5\n" + + " \n" + + " \n" + + ""; + ByteArrayOutputStream pdf = iFToPDF(ifXml, fopxconf); + Assert.assertTrue(pdf.toString().contains("/S /GoTo")); + } } diff --git a/fop-core/src/test/resources/org/apache/fop/render/extensions/linkmissingdest.if.xml b/fop-core/src/test/resources/org/apache/fop/render/extensions/linkmissingdest.if.xml new file mode 100644 index 000000000..f2e6eacb0 --- /dev/null +++ b/fop-core/src/test/resources/org/apache/fop/render/extensions/linkmissingdest.if.xml @@ -0,0 +1,42 @@ + + +
+ + + + +creator +Document title + + + +
+ + + + + + + + + + + + + + + + + +test + + + + + + + + + + +
-- cgit v1.2.3