|
| 1 | +/* |
| 2 | + This file is part of the iText (R) project. |
| 3 | + Copyright (c) 1998-2023 Apryse Group NV |
| 4 | + Authors: Apryse Software. |
| 5 | +
|
| 6 | + This program is offered under a commercial and under the AGPL license. |
| 7 | + For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below. |
| 8 | +
|
| 9 | + AGPL licensing: |
| 10 | + This program is free software: you can redistribute it and/or modify |
| 11 | + it under the terms of the GNU Affero General Public License as published by |
| 12 | + the Free Software Foundation, either version 3 of the License, or |
| 13 | + (at your option) any later version. |
| 14 | +
|
| 15 | + This program is distributed in the hope that it will be useful, |
| 16 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | + GNU Affero General Public License for more details. |
| 19 | +
|
| 20 | + You should have received a copy of the GNU Affero General Public License |
| 21 | + along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 22 | + */ |
| 23 | +package com.itextpdf.html2pdf; |
| 24 | + |
| 25 | +import com.itextpdf.html2pdf.resolver.font.DefaultFontProvider; |
| 26 | +import com.itextpdf.kernel.pdf.PdfDocument; |
| 27 | +import com.itextpdf.kernel.pdf.PdfDocumentInfo; |
| 28 | +import com.itextpdf.kernel.pdf.PdfString; |
| 29 | +import com.itextpdf.kernel.pdf.PdfVersion; |
| 30 | +import com.itextpdf.kernel.pdf.PdfViewerPreferences; |
| 31 | +import com.itextpdf.kernel.pdf.PdfWriter; |
| 32 | +import com.itextpdf.kernel.pdf.WriterProperties; |
| 33 | +import com.itextpdf.kernel.utils.CompareTool; |
| 34 | +import com.itextpdf.kernel.xmp.XMPException; |
| 35 | +import com.itextpdf.kernel.xmp.XMPMeta; |
| 36 | +import com.itextpdf.kernel.xmp.XMPMetaFactory; |
| 37 | +import com.itextpdf.layout.font.FontProvider; |
| 38 | +import com.itextpdf.test.ExtendedITextTest; |
| 39 | +import com.itextpdf.test.annotations.type.IntegrationTest; |
| 40 | +import com.itextpdf.test.pdfa.VeraPdfValidator; |
| 41 | + |
| 42 | +import java.io.ByteArrayInputStream; |
| 43 | +import java.io.FileInputStream; |
| 44 | +import java.io.IOException; |
| 45 | +import java.nio.file.Files; |
| 46 | +import java.nio.file.Paths; |
| 47 | +import org.junit.Assert; |
| 48 | +import org.junit.BeforeClass; |
| 49 | +import org.junit.Test; |
| 50 | +import org.junit.experimental.categories.Category; |
| 51 | + |
| 52 | +@Category(IntegrationTest.class) |
| 53 | +public class HtmlConverterPdfUA2Test extends ExtendedITextTest { |
| 54 | + |
| 55 | + private static final String SOURCE_FOLDER = "./src/test/resources/com/itextpdf/html2pdf/HtmlConverterPdfUA2Test/"; |
| 56 | + private static final String DESTINATION_FOLDER = "./target/test/com/itextpdf/html2pdf/HtmlConverterPdfUA2Test/"; |
| 57 | + |
| 58 | + @BeforeClass |
| 59 | + public static void beforeClass() { |
| 60 | + createOrClearDestinationFolder(DESTINATION_FOLDER); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void simpleLinkTest() throws IOException, InterruptedException, XMPException { |
| 65 | + String sourceHtml = SOURCE_FOLDER + "simpleLink.html"; |
| 66 | + String cmpPdf = SOURCE_FOLDER + "cmp_simpleLink.pdf"; |
| 67 | + String destinationPdf = DESTINATION_FOLDER + "simpleLink.pdf"; |
| 68 | + |
| 69 | + PdfDocument pdfDocument = new PdfDocument(new PdfWriter(destinationPdf, new WriterProperties().setPdfVersion( |
| 70 | + PdfVersion.PDF_2_0))); |
| 71 | + createSimplePdfUA2Document(pdfDocument); |
| 72 | + |
| 73 | + ConverterProperties converterProperties = new ConverterProperties(); |
| 74 | + FontProvider fontProvider = new DefaultFontProvider(false, true, false); |
| 75 | + converterProperties.setFontProvider(fontProvider); |
| 76 | + HtmlConverter.convertToPdf(new FileInputStream(sourceHtml), pdfDocument, converterProperties); |
| 77 | + |
| 78 | + /* TODO: DEVSIX-7996 - Links created from html2pdf are not ua-2 compliant |
| 79 | + * Two verapdf errors are generated here: |
| 80 | + * 1. clause="8.9.4.1", Link annotation neither has a Contents entry nor alternate description. |
| 81 | + * 2. clause="8.5.1", Real content that does not possess the semantics of text objects and does not have |
| 82 | + * an alternate textual representation is not enclosed within Figure or Formula structure elements. |
| 83 | + */ |
| 84 | + compareAndCheckCompliance(destinationPdf, cmpPdf, false); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void backwardLinkTest() throws IOException, InterruptedException, XMPException { |
| 89 | + String sourceHtml = SOURCE_FOLDER + "backwardLink.html"; |
| 90 | + String cmpPdf = SOURCE_FOLDER + "cmp_backwardLink.pdf"; |
| 91 | + String destinationPdf = DESTINATION_FOLDER + "backwardLink.pdf"; |
| 92 | + |
| 93 | + PdfDocument pdfDocument = new PdfDocument(new PdfWriter(destinationPdf, new WriterProperties().setPdfVersion( |
| 94 | + PdfVersion.PDF_2_0))); |
| 95 | + createSimplePdfUA2Document(pdfDocument); |
| 96 | + |
| 97 | + ConverterProperties converterProperties = new ConverterProperties(); |
| 98 | + FontProvider fontProvider = new DefaultFontProvider(false, true, false); |
| 99 | + converterProperties.setFontProvider(fontProvider); |
| 100 | + HtmlConverter.convertToPdf(new FileInputStream(sourceHtml), pdfDocument, converterProperties); |
| 101 | + |
| 102 | + /* TODO: DEVSIX-7996 - Links created from html2pdf are not ua-2 compliant |
| 103 | + * Two verapdf errors are generated here: |
| 104 | + * 1. clause="8.9.4.1", Link annotation neither has a Contents entry nor alternate description. |
| 105 | + * 2. clause="8.5.1", Real content that does not possess the semantics of text objects and does not have |
| 106 | + * an alternate textual representation is not enclosed within Figure or Formula structure elements. |
| 107 | + */ |
| 108 | + compareAndCheckCompliance(destinationPdf, cmpPdf, false); |
| 109 | + } |
| 110 | + |
| 111 | + private void createSimplePdfUA2Document(PdfDocument pdfDocument) throws IOException, XMPException { |
| 112 | + byte[] bytes = Files.readAllBytes(Paths.get(SOURCE_FOLDER + "simplePdfUA2.xmp")); |
| 113 | + XMPMeta xmpMeta = XMPMetaFactory.parse(new ByteArrayInputStream(bytes)); |
| 114 | + pdfDocument.setXmpMetadata(xmpMeta); |
| 115 | + pdfDocument.setTagged(); |
| 116 | + pdfDocument.getCatalog().setViewerPreferences(new PdfViewerPreferences().setDisplayDocTitle(true)); |
| 117 | + pdfDocument.getCatalog().setLang(new PdfString("en-US")); |
| 118 | + PdfDocumentInfo info = pdfDocument.getDocumentInfo(); |
| 119 | + info.setTitle("PdfUA2 Title"); |
| 120 | + } |
| 121 | + |
| 122 | + private static void compareAndCheckCompliance(String destinationPdf, String cmpPdf, boolean isExpectedOk) |
| 123 | + throws IOException, InterruptedException { |
| 124 | + if (isExpectedOk) { |
| 125 | + Assert.assertNull(new VeraPdfValidator().validate(destinationPdf)); |
| 126 | + } else { |
| 127 | + Assert.assertNotNull(new VeraPdfValidator().validate(destinationPdf)); |
| 128 | + } |
| 129 | + Assert.assertNull(new CompareTool().compareByContent(destinationPdf, cmpPdf, DESTINATION_FOLDER, |
| 130 | + "diff_simple_")); |
| 131 | + } |
| 132 | +} |
0 commit comments