validate-pdf: find the PDF/X-4 XMP properties by namespace (#3144)

The PDF/X-4 check looked for the text `pdfxid:GTS_PDFXVersion>PDF/X-4<`,
`<xmp:MetadataDate>` and `<xmpMM:VersionID>` anywhere in the file, so a
packet that declares the namespace on the property, binds it to another
prefix or writes the property as an attribute of rdf:Description was
reported as not naming PDF/X-4.

Read the catalog's XMP packet with craft-xml, naming its elements and
attributes by namespace URI, and look each property up as an element or
an attribute.
This commit is contained in:
Zinadin Zidan committed 2026-12-02 01:08:08 +06:00
1 parent 2866101f38
commit 2bc4c0708e
4 files changed
+92 -3

No files matched your search

Generated
+1
View File
@@ -2519,6 +2519,7 @@ dependencies = [
"craft-image-codecs",
"craft-pdf",
"craft-prepress",
"craft-xml",
"designcraft-color",
"designcraft-compose",
"designcraft-doc",
+1
View File
@@ -19,6 +19,7 @@ craft-pdf = { workspace = true, features = ["read", "write-pdf", "write-svg"] }
craft-image-codecs = { workspace = true }
craft-prepress = { workspace = true }
craft-cms = { workspace = true }
craft-xml = { workspace = true }
designcraft-images = { workspace = true }
image = { workspace = true }
thiserror = { workspace = true }
+27 -3
View File
@@ -158,13 +158,15 @@ pub fn check_pdfx4(pdf: &[u8]) -> Vec<String> {
if file.as_ref().and_then(|f| info_text(pdf, f, b"GTS_PDFXVersion")).as_deref() != Some("PDF/X-4") {
issues.push("the document information doesn't name PDF/X-4".into());
}
if !text.contains("pdfxid:GTS_PDFXVersion>PDF/X-4<") {
let xmp = file.as_ref().and_then(xmp_metadata);
let property = |name: &str| xmp.as_ref().and_then(|x| xmp_property(x, name));
if property("pdfxid:GTS_PDFXVersion").as_deref() != Some("PDF/X-4") {
issues.push("the XMP metadata doesn't name PDF/X-4".into());
}
if !text.contains("<xmp:MetadataDate>") {
if property("xmp:MetadataDate").is_none() {
issues.push("the XMP metadata has no xmp:MetadataDate".into());
}
if !text.contains("<xmpMM:VersionID>") {
if property("xmpMM:VersionID").is_none() {
issues.push("the XMP metadata has no xmpMM:VersionID".into());
}
if text.contains("/Interpolate true") {
@@ -192,6 +194,28 @@ pub fn pdfx_version(pdf: &[u8]) -> Option<String> {
info_text(pdf, &craft_pdf::syntax::Pdf::new(pdf.to_vec()).ok()?, b"GTS_PDFXVersion")
}
/// The XMP namespaces the PDF/X-4 check reads, by URI, with the prefixes it names them by.
const XMP_NAMESPACES: &[(&str, &str)] =
&[("pdfxid", "http://www.npes.org/pdfx/ns/id/"), ("xmp", "http://ns.adobe.com/xap/1.0/"), ("xmpMM", "http://ns.adobe.com/xap/1.0/mm/")];
/// The catalog's XMP metadata packet as XML, its names in [`XMP_NAMESPACES`] given those
/// prefixes whatever prefix the packet binds them to and wherever it declares them.
fn xmp_metadata(file: &craft_pdf::syntax::Pdf) -> Option<craft_xml::El> {
let xref = file.xref();
let catalog = xref.get::<Dict<'_>>(xref.root_id())?;
let data = catalog.get::<Stream<'_>>(b"Metadata")?.decoded().ok()?;
let names = craft_xml::Canonical::new(XMP_NAMESPACES);
craft_xml::Options::canonical(&names).parse(&data).ok()
}
/// The value of the XMP property `name` (`prefix:local`, a prefix of [`XMP_NAMESPACES`]),
/// trimmed: written as an element, or as an attribute (the shorthand on `rdf:Description`).
fn xmp_property(xmp: &craft_xml::El, name: &str) -> Option<String> {
std::iter::once(xmp)
.chain(xmp.descendants())
.find_map(|el| if el.name == name { Some(el.text().trim().to_string()) } else { el.attr(name).map(|v| v.trim().to_string()) })
}
/// The catalog's `GTS_PDFX` output intents, read as objects (whitespace and object streams
/// don't matter).
pub(crate) fn pdfx_intents<'a>(file: &'a craft_pdf::syntax::Pdf) -> Vec<Dict<'a>> {
+63
View File
@@ -666,6 +666,69 @@ fn x4_checks_ignore_whitespace_between_tokens() {
assert!(issues.iter().any(|i| i == NO_INTENT) && issues.iter().any(|i| i == NO_INFO), "{issues:?}");
}
const NO_XMP_VERSION: &str = "the XMP metadata doesn't name PDF/X-4";
const NO_XMP_DATE: &str = "the XMP metadata has no xmp:MetadataDate";
const PDFXID: &str = "http://www.npes.org/pdfx/ns/id/";
const XMP: &str = "http://ns.adobe.com/xap/1.0/";
/// The PDF/X-4 issues of a file whose catalog metadata is an XMP packet holding `description`.
fn xmp_issues(description: &str) -> Vec<String> {
let packet = format!(
"<?xpacket begin=\"\u{feff}\" id=\"W5M0MpCehiHzreSzNTczkc9d\"?><x:xmpmeta xmlns:x=\"adobe:ns:meta/\">\
<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">{description}</rdf:RDF></x:xmpmeta><?xpacket end=\"w\"?>"
);
let metadata = format!("<</Type/Metadata/Subtype/XML/Length {}>>\nstream\n{packet}\nendstream", packet.len());
check_pdfx4(&assemble_x4(
&["<</Type/Catalog/Pages 2 0 R/Metadata 3 0 R>>", "<</Type/Pages/Kids[]/Count 0>>", &metadata],
"<</Size 4/Root 1 0 R>>",
))
}
/// The XMP's PDF/X version and metadata date are found by namespace URI (#3144): declared on the
/// property itself, bound to another prefix, or written as attributes of `rdf:Description`.
#[test]
fn xmp_properties_are_found_by_namespace_whatever_the_prefix_or_form() {
let date = "2026-01-02T03:04:05Z";
let packets = [
format!(
"<rdf:Description rdf:about=\"\"><pdfxid:GTS_PDFXVersion xmlns:pdfxid=\"{PDFXID}\">PDF/X-4</pdfxid:GTS_PDFXVersion>\
<xmp:MetadataDate xmlns:xmp=\"{XMP}\">{date}</xmp:MetadataDate></rdf:Description>"
),
format!(
"<rdf:Description rdf:about=\"\" xmlns:x4=\"{PDFXID}\" xmlns:meta=\"{XMP}\">\
<x4:GTS_PDFXVersion>PDF/X-4</x4:GTS_PDFXVersion><meta:MetadataDate>{date}</meta:MetadataDate></rdf:Description>"
),
format!(
"<rdf:Description rdf:about=\"\" xmlns:pdfxid=\"{PDFXID}\" xmlns:xmp=\"{XMP}\" \
pdfxid:GTS_PDFXVersion=\"PDF/X-4\" xmp:MetadataDate=\"{date}\"/>"
),
];
for packet in packets {
let issues = xmp_issues(&packet);
assert!(!issues.iter().any(|i| i == NO_XMP_VERSION || i == NO_XMP_DATE), "{packet}: {issues:?}");
}
}
/// A packet that names another PDF/X version, lacks the properties, or uses their prefixes for
/// other namespaces still fails.
#[test]
fn xmp_without_a_pdfx4_version_or_metadata_date_fails() {
let packets = [
format!(
"<rdf:Description rdf:about=\"\" xmlns:pdfxid=\"{PDFXID}\"><pdfxid:GTS_PDFXVersion>PDF/X-1a:2003</pdfxid:GTS_PDFXVersion></rdf:Description>"
),
"<rdf:Description rdf:about=\"\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\"><dc:format>application/pdf</dc:format></rdf:Description>"
.to_string(),
"<rdf:Description rdf:about=\"\" xmlns:pdfxid=\"http://example.com/other/\" xmlns:xmp=\"http://example.com/xap/\">\
<pdfxid:GTS_PDFXVersion>PDF/X-4</pdfxid:GTS_PDFXVersion><xmp:MetadataDate>2026-01-02T03:04:05Z</xmp:MetadataDate></rdf:Description>"
.to_string(),
];
for packet in packets {
let issues = xmp_issues(&packet);
assert!(issues.iter().any(|i| i == NO_XMP_VERSION) && issues.iter().any(|i| i == NO_XMP_DATE), "{packet}: {issues:?}");
}
}
/// The output intent and the document information are found inside a compressed object stream,
/// through a cross-reference stream.
#[test]