Skip to content

Commit 94dfab5

Browse files
Correct XMP URI serialisation in PDF/UA ext schema
URIs and text follow different conventions in the XMP data model. While many parsers tolerate URIs encoded as text, it's technically not allowed.
1 parent 475fcdd commit 94dfab5

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

pdfa/src/main/java/com/itextpdf/pdfa/PdfAXMPUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public class PdfAXMPUtil {
5454
" <pdfaExtension:schemas>\n" +
5555
" <rdf:Bag>\n" +
5656
" <rdf:li rdf:parseType=\"Resource\">\n" +
57-
" <pdfaSchema:namespaceURI>http://www.aiim.org/pdfua/ns/id/</pdfaSchema:namespaceURI>\n" +
57+
" <pdfaSchema:namespaceURI rdf:resource=\"http://www.aiim.org/pdfua/ns/id/\"/>\n" +
5858
" <pdfaSchema:prefix>pdfuaid</pdfaSchema:prefix>\n" +
5959
" <pdfaSchema:schema>PDF/UA identification schema</pdfaSchema:schema>\n" +
6060
" <pdfaSchema:property>\n" +

pdfa/src/test/java/com/itextpdf/pdfa/PdfAXmpTest.java

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

45+
import com.itextpdf.io.source.ByteArrayOutputStream;
4546
import com.itextpdf.kernel.pdf.PdfAConformanceLevel;
4647
import com.itextpdf.kernel.pdf.PdfDocument;
4748
import com.itextpdf.kernel.pdf.PdfOutputIntent;
4849
import com.itextpdf.kernel.pdf.PdfReader;
50+
import com.itextpdf.kernel.pdf.PdfString;
51+
import com.itextpdf.kernel.pdf.PdfViewerPreferences;
4952
import com.itextpdf.kernel.pdf.PdfWriter;
53+
import com.itextpdf.kernel.pdf.WriterProperties;
5054
import com.itextpdf.kernel.utils.CompareTool;
5155
import com.itextpdf.kernel.xmp.XMPConst;
5256
import com.itextpdf.kernel.xmp.XMPException;
@@ -56,9 +60,15 @@ This file is part of the iText (R) project.
5660
import com.itextpdf.kernel.xmp.options.SerializeOptions;
5761
import com.itextpdf.test.ExtendedITextTest;
5862
import com.itextpdf.test.annotations.type.IntegrationTest;
63+
64+
import java.io.ByteArrayInputStream;
5965
import java.io.FileInputStream;
66+
import java.io.FileOutputStream;
6067
import java.io.IOException;
6168
import java.io.InputStream;
69+
import java.io.OutputStream;
70+
import java.nio.charset.StandardCharsets;
71+
6272
import org.junit.Assert;
6373
import org.junit.BeforeClass;
6474
import org.junit.Test;
@@ -147,6 +157,57 @@ public void saveAndReadDocumentWithCanonicalXmpMetadata() throws IOException, XM
147157
}
148158
}
149159

160+
@Test
161+
public void testPdfUAExtensionMetadata() throws IOException {
162+
163+
String outFile = destinationFolder + "testPdfUAExtensionMetadata.pdf";
164+
String cmpFile = cmpFolder + "cmp_testPdfUAExtensionMetadata.pdf";
165+
166+
try (FileOutputStream fos = new FileOutputStream(outFile)) {
167+
generatePdfAWithUA(fos);
168+
}
169+
170+
CompareTool ct = new CompareTool();
171+
Assert.assertNull(ct.compareXmp(outFile, cmpFile, true));
172+
173+
}
174+
175+
@Test
176+
public void testPdfUAIdSchemaNameSpaceUriIsNotText() throws IOException {
177+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
178+
generatePdfAWithUA(baos);
179+
180+
// check whether the pdfuaid NS URI was properly encoded as a URI with rdf:resource
181+
PdfDocument readDoc = new PdfDocument(new PdfReader(new ByteArrayInputStream(baos.toByteArray())));
182+
String xmpString = new String(readDoc.getXmpMetadata(), StandardCharsets.UTF_8);
183+
Assert.assertTrue(
184+
"Did not find expected namespaceURI definition",
185+
xmpString.contains("<pdfaSchema:namespaceURI rdf:resource=\"http://www.aiim.org/pdfua/ns/id/\"/>")
186+
);
187+
188+
}
189+
190+
private void generatePdfAWithUA(OutputStream os) throws IOException {
191+
WriterProperties wp = new WriterProperties().addUAXmpMetadata();
192+
try (PdfWriter w = new PdfWriter(os, wp)) {
193+
PdfOutputIntent outputIntent;
194+
try (InputStream is = new FileInputStream(sourceFolder + "sRGB Color Space Profile.icm")) {
195+
outputIntent = new PdfOutputIntent(
196+
"Custom", "",
197+
"http://www.color.org",
198+
"sRGB IEC61966-2.1",
199+
is
200+
);
201+
}
202+
PdfDocument pdfDoc = new PdfADocument(w, PdfAConformanceLevel.PDF_A_2A, outputIntent).setTagged();
203+
pdfDoc.getDocumentInfo().setTitle("Test document");
204+
pdfDoc.getCatalog().setViewerPreferences(new PdfViewerPreferences().setDisplayDocTitle(true));
205+
pdfDoc.getCatalog().setLang(new PdfString("en"));
206+
pdfDoc.addNewPage();
207+
pdfDoc.close();
208+
}
209+
}
210+
150211
private int count(byte[] array, byte b) {
151212
int counter = 0;
152213
for (byte each : array) {
Binary file not shown.

0 commit comments

Comments
 (0)