Skip to content

Commit

Permalink
close #433: Cannot update XMP metadata when producer/creator contains…
Browse files Browse the repository at this point in the history
… 0x0 character
  • Loading branch information
ediweissmann committed May 31, 2024
1 parent 73f2b26 commit 01940f7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,8 @@ private void updateTextNode(String tagName, Document document, String value) thr
}

if(node != null) {
node.setTextContent(value);
String sanitized = value.replaceAll("\u0000", "");
node.setTextContent(sanitized);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,33 @@ public void updatedXmpMetadata() throws IOException {
}
});
}

@Test
public void invalidXmlChars_updatedXmpMetadata() throws IOException {
setUpParams(stronglyEncryptedInput());
parameters.put(PdfMetadataFields.CREATOR, "test_creator\u0000");
parameters.put("Producer", "test_\u0000producer");

parameters.setExistingOutputPolicy(ExistingOutputPolicy.OVERWRITE);
testContext.directoryOutputTo(parameters);

execute(parameters);

testContext.assertTaskCompleted();
testContext.forEachPdfOutput(document -> {
try {
DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
DocumentBuilder b = f.newDocumentBuilder();
Document xmlDoc = b.parse(document.getDocumentCatalog().getMetadata().createInputStream());

assertEquals("test_producer", getNodeValue(xmlDoc, "//*[name()='pdf:Producer']"));
assertEquals("test_creator", getNodeValue(xmlDoc, "//*[name()='xmp:CreatorTool']"));

} catch (Exception e) {
throw new RuntimeException(e);
}
});
}

@Test
public void xmpMetadataMissingNamespaceAndWithAttributesInsteadofNodes() throws IOException {
Expand Down

0 comments on commit 01940f7

Please sign in to comment.