diff --git a/crates/pdf/src/pdfx.rs b/crates/pdf/src/pdfx.rs index a8f85a2873..d1741227f9 100644 --- a/crates/pdf/src/pdfx.rs +++ b/crates/pdf/src/pdfx.rs @@ -89,11 +89,15 @@ pub(crate) fn xmp(pdf: &[u8], xref: &Xref, root: (usize, usize), patch: &mut Pat let xml = data.checked_add(length).and_then(|e| obj.get(data..e)).ok_or_else(bad)?; let mut edits: Vec<(usize, usize, Vec)> = vec![]; if let Some((version, _)) = pdfx_ids(standard).filter(|_| standard == Standard::PdfX4) { - // Into the writer's description, whose namespaces declare the `pdf` and `xmpMM` prefixes. + // The writer wraps its properties in one outer description. Declare `pdfxid` there with + // the other namespaces, so validators can find the version property without attributes. + let description = b"").ok_or_else(bad)?; + edits.push((open, open, br#" xmlns:pdfxid="http://www.npes.org/pdfx/ns/id/""#.to_vec())); let trapped = if trapped { "True" } else { "False" }; let props = format!( - "{version}{trapped}1" + "{version}{trapped}1" ); edits.push((close, close, props.into_bytes())); // When the packet has no `` element, this adds one after the writer's diff --git a/crates/pdf/src/tests/pdfx.rs b/crates/pdf/src/tests/pdfx.rs index 6af2e0e0a5..f9d6e90b5a 100644 --- a/crates/pdf/src/tests/pdfx.rs +++ b/crates/pdf/src/tests/pdfx.rs @@ -120,7 +120,16 @@ fn each_standard_writes_its_header_output_intent_boxes_and_identification() { // The metadata agrees with the header; PDF/X-4 names its standard there too. let xmp_version = if standard == Standard::PdfX4 { "1.6" } else { "1.3" }; assert!(t.contains(&format!("{xmp_version}")), "{standard:?}"); - assert_eq!(t.contains("PDF/X-4<"), standard == Standard::PdfX4); + let xmp = xmp_packet(&r.bytes); + let (_, description) = xmp.rsplit_once("').expect("the end of the opening tag"); + let is_pdfx4 = standard == Standard::PdfX4; + // Validators expect the version property to inherit its namespace from the description. + assert_eq!(attributes.contains(r#"xmlns:pdfxid="http://www.npes.org/pdfx/ns/id/""#), is_pdfx4, "{standard:?}: {attributes}"); + assert_eq!(xmp_value(&xmp, "pdfxid:GTS_PDFXVersion"), is_pdfx4.then_some("PDF/X-4"), "{standard:?}"); + // The edited packet is still well-formed XML: the namespace is declared once. + assert_eq!(attributes.matches("xmlns:pdfxid=").count(), usize::from(is_pdfx4), "{standard:?}: {attributes}"); + assert!(craft_xml::Options::default().strict().parse(xmp.as_bytes()).is_ok(), "{standard:?}: {xmp}"); assert_eq!(t.contains("False"), standard == Standard::PdfX4); assert!(!t.contains("/Encrypt"), "{standard:?}"); // The file reads back.