Skip to content

Commit

Permalink
Correct XMP URI serialisation in PDF/UA ext schema
Browse files Browse the repository at this point in the history
URIs and text follow different conventions in the XMP data model. While
many parsers tolerate URIs encoded as text, it's technically not
allowed.
  • Loading branch information
MatthiasValvekens committed Sep 14, 2022
1 parent 475fcdd commit 190df80
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pdfa/src/main/java/com/itextpdf/pdfa/PdfAXMPUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class PdfAXMPUtil {
" <pdfaExtension:schemas>\n" +
" <rdf:Bag>\n" +
" <rdf:li rdf:parseType=\"Resource\">\n" +
" <pdfaSchema:namespaceURI>http://www.aiim.org/pdfua/ns/id/</pdfaSchema:namespaceURI>\n" +
" <pdfaSchema:namespaceURI rdf:resource=\"http://www.aiim.org/pdfua/ns/id/\"/>\n" +
" <pdfaSchema:prefix>pdfuaid</pdfaSchema:prefix>\n" +
" <pdfaSchema:schema>PDF/UA identification schema</pdfaSchema:schema>\n" +
" <pdfaSchema:property>\n" +
Expand Down
67 changes: 59 additions & 8 deletions pdfa/src/test/java/com/itextpdf/pdfa/PdfAXmpTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,23 @@ This file is part of the iText (R) project.
*/
package com.itextpdf.pdfa;

import com.itextpdf.kernel.pdf.PdfAConformanceLevel;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfOutputIntent;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.io.source.ByteArrayOutputStream;
import com.itextpdf.kernel.pdf.*;
import com.itextpdf.kernel.utils.CompareTool;
import com.itextpdf.kernel.xmp.XMPConst;
import com.itextpdf.kernel.xmp.XMPException;
import com.itextpdf.kernel.xmp.XMPMeta;
import com.itextpdf.kernel.xmp.XMPMetaFactory;
import com.itextpdf.kernel.xmp.options.ParseOptions;
import com.itextpdf.kernel.xmp.options.PropertyOptions;
import com.itextpdf.kernel.xmp.options.SerializeOptions;
import com.itextpdf.kernel.xmp.properties.XMPProperty;
import com.itextpdf.test.ExtendedITextTest;
import com.itextpdf.test.annotations.type.IntegrationTest;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import java.io.*;
import java.nio.charset.StandardCharsets;

import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
Expand Down Expand Up @@ -147,6 +147,57 @@ public void saveAndReadDocumentWithCanonicalXmpMetadata() throws IOException, XM
}
}

@Test
public void testPdfUAExtensionMetadata() throws IOException {

String outFile = destinationFolder + "testPdfUAExtensionMetadata.pdf";
String cmpFile = cmpFolder + "cmp_testPdfUAExtensionMetadata.pdf";

try (FileOutputStream fos = new FileOutputStream(outFile)) {
generatePdfAWithUA(fos);
}

CompareTool ct = new CompareTool();
Assert.assertNull(ct.compareXmp(outFile, cmpFile, true));

}

@Test
public void testPdfUAIdSchemaNameSpaceUriIsNotText() throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
generatePdfAWithUA(baos);

// check whether the pdfuaid NS URI was properly encoded as a URI with rdf:resource
PdfDocument readDoc = new PdfDocument(new PdfReader(new ByteArrayInputStream(baos.toByteArray())));
String xmpString = new String(readDoc.getXmpMetadata(), StandardCharsets.UTF_8);
Assert.assertTrue(
"Did not find expected namespaceURI definition",
xmpString.contains("<pdfaSchema:namespaceURI rdf:resource=\"http://www.aiim.org/pdfua/ns/id/\"/>")
);

}

private void generatePdfAWithUA(OutputStream os) throws IOException {
WriterProperties wp = new WriterProperties().addUAXmpMetadata();
try (PdfWriter w = new PdfWriter(os, wp)) {
PdfOutputIntent outputIntent;
try (InputStream is = new FileInputStream(sourceFolder + "sRGB Color Space Profile.icm")) {
outputIntent = new PdfOutputIntent(
"Custom", "",
"http://www.color.org",
"sRGB IEC61966-2.1",
is
);
}
PdfDocument pdfDoc = new PdfADocument(w, PdfAConformanceLevel.PDF_A_2A, outputIntent).setTagged();
pdfDoc.getDocumentInfo().setTitle("Test document");
pdfDoc.getCatalog().setViewerPreferences(new PdfViewerPreferences().setDisplayDocTitle(true));
pdfDoc.getCatalog().setLang(new PdfString("en"));
pdfDoc.addNewPage();
pdfDoc.close();
}
}

private int count(byte[] array, byte b) {
int counter = 0;
for (byte each : array) {
Expand Down
Binary file not shown.

0 comments on commit 190df80

Please sign in to comment.