From 31360580372f0723cfae475f7aba564fe7b25fa1 Mon Sep 17 00:00:00 2001 From: danfickle Date: Mon, 4 Mar 2019 20:53:40 +1100 Subject: [PATCH] #326 - Yeah! We are now PDF/A1a compliant. Had to move the thead, tbody and tfoot rows directly as children of the table when using PDF version 1.4 or less. --- .../pdfa/testing/PdfATester.java | 7 +++--- .../pdfboxout/PdfBoxAccessibilityHelper.java | 22 +++++++++++++++---- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/openhtmltopdf-pdfa-testing/src/test/java/com/openhtmltopdf/pdfa/testing/PdfATester.java b/openhtmltopdf-pdfa-testing/src/test/java/com/openhtmltopdf/pdfa/testing/PdfATester.java index e0d43a6f4..3a1461a6b 100644 --- a/openhtmltopdf-pdfa-testing/src/test/java/com/openhtmltopdf/pdfa/testing/PdfATester.java +++ b/openhtmltopdf-pdfa-testing/src/test/java/com/openhtmltopdf/pdfa/testing/PdfATester.java @@ -18,7 +18,6 @@ import org.apache.pdfbox.io.IOUtils; import org.junit.BeforeClass; -import org.junit.Ignore; import org.junit.Test; import org.verapdf.pdfa.Foundries; import org.verapdf.pdfa.PDFAParser; @@ -61,7 +60,8 @@ public boolean run(String resource, PDFAFlavour flavour, PdfAConformance conform PdfRendererBuilder builder = new PdfRendererBuilder(); builder.useFastMode(); - builder.testMode(true); + //builder.testMode(true); + builder.usePdfVersion(conform.getPart() == 1 ? 1.4f : 1.5f); builder.usePdfAConformance(conform); builder.useFont(new File("target/test/artefacts/Karla-Bold.ttf"), "TestFont"); builder.withHtmlContent(html, PdfATester.class.getResource("/html/").toString()); @@ -109,8 +109,7 @@ public void testAllInOnePdfA1b() throws Exception { assertTrue(run("all-in-one-no-alpha", PDFAFlavour.PDFA_1_B, PdfAConformance.PDFA_1_B)); } - @Ignore // Failing because TFoot, Tbody and THead strcuture types were only introduced with PDF 1.5. - @Test // We have to factor them out when using PDF/A1a. + @Test public void testAllInOnePdfA1a() throws Exception { assertTrue(run("all-in-one-no-alpha", PDFAFlavour.PDFA_1_A, PdfAConformance.PDFA_1_A)); } diff --git a/openhtmltopdf-pdfbox/src/main/java/com/openhtmltopdf/pdfboxout/PdfBoxAccessibilityHelper.java b/openhtmltopdf-pdfbox/src/main/java/com/openhtmltopdf/pdfboxout/PdfBoxAccessibilityHelper.java index 549e5aadf..135cb459c 100644 --- a/openhtmltopdf-pdfbox/src/main/java/com/openhtmltopdf/pdfboxout/PdfBoxAccessibilityHelper.java +++ b/openhtmltopdf-pdfbox/src/main/java/com/openhtmltopdf/pdfboxout/PdfBoxAccessibilityHelper.java @@ -377,6 +377,7 @@ private static class TableStructualElement extends AbstractStructualElement { final TableHeadStructualElement thead = new TableHeadStructualElement(); final List tbodies = new ArrayList<>(1); final TableFootStructualElement tfoot = new TableFootStructualElement(); + float pdfVersion = 1.5f; @Override void addChild(AbstractTreeItem child) { @@ -387,6 +388,10 @@ void addChild(AbstractTreeItem child) { } } + void setPdfVersion(float version) { + this.pdfVersion = version; + } + @Override String getPdfTag() { return StandardStructureTypes.TABLE; @@ -398,9 +403,17 @@ void finish(AbstractStructualElement parent) { createPdfStrucureElement(parent, child); - finishTreeItem(child.thead, child); - finishTreeItems(child.tbodies, child); - finishTreeItem(child.tfoot, child); + if (pdfVersion < 1.5f) { + // THead, TBody and TFoot were introduced in PDF 1.5 so if we can not use + // them then we process their rows directly and add them to the table. + finishTreeItems(child.thead.children, child); + child.tbodies.forEach(tbody -> finishTreeItems(tbody.children, child)); + finishTreeItems(child.tfoot.children, child); + } else { + finishTreeItem(child.thead, child); + finishTreeItems(child.tbodies, child); + finishTreeItem(child.tfoot, child); + } } } @@ -659,7 +672,6 @@ private static class FigureContentItem extends GenericContentItem { } private static void logIncompatibleChild(AbstractTreeItem parent, AbstractTreeItem child, Class expected) { - System.out.println("OH OH"); XRLog.general(Level.WARNING, "Trying to add incompatible child to parent item: " + " child type=" + child.getClass().getSimpleName() + @@ -836,6 +848,8 @@ private AbstractStructualElement createStructureItem(StructureType type, Box box TableStructualElement table = (TableStructualElement) box.getParent().getAccessibilityObject(); + table.setPdfVersion(_od.getWriter().getVersion()); + if (box.getStyle().isIdent(CSSName.DISPLAY, IdentValue.TABLE_HEADER_GROUP)) { child = table.thead; } else if (box.getStyle().isIdent(CSSName.DISPLAY, IdentValue.TABLE_ROW_GROUP)) {