Merge pull request #9937 from dotCooCoo/train/carry9168

PDF: PDF/X-4 exports declare the identification namespace on the XMP description (carries #9168)
This commit is contained in:
.CooCoo authored and GitHub committed 2026-11-28 08:36:48 -08:00
commit 4fe3b646f7
2 files changed
+16 -3

No files matched your search

+6 -2
View File
@@ -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<u8>)> = 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"<rdf:Description";
let open = find(xml, description, 0).ok_or_else(bad)? + description.len();
let close = crate::lab_spot::rfind(xml, b"</rdf:Description>").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!(
"<pdfxid:GTS_PDFXVersion xmlns:pdfxid=\"http://www.npes.org/pdfx/ns/id/\">{version}</pdfxid:GTS_PDFXVersion><pdf:Trapped>{trapped}</pdf:Trapped><xmpMM:VersionID>1</xmpMM:VersionID>"
"<pdfxid:GTS_PDFXVersion>{version}</pdfxid:GTS_PDFXVersion><pdf:Trapped>{trapped}</pdf:Trapped><xmpMM:VersionID>1</xmpMM:VersionID>"
);
edits.push((close, close, props.into_bytes()));
// When the packet has no `<xmp:MetadataDate>` element, this adds one after the writer's
+10 -1
View File
@@ -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!("<pdf:PDFVersion>{xmp_version}</pdf:PDFVersion>")), "{standard:?}");
assert_eq!(t.contains("<pdfxid:GTS_PDFXVersion xmlns:pdfxid=\"http://www.npes.org/pdfx/ns/id/\">PDF/X-4<"), standard == Standard::PdfX4);
let xmp = xmp_packet(&r.bytes);
let (_, description) = xmp.rsplit_once("<rdf:Description").expect("an XMP description");
let (attributes, _) = description.split_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("<pdf:Trapped>False</pdf:Trapped>"), standard == Standard::PdfX4);
assert!(!t.contains("/Encrypt"), "{standard:?}");
// The file reads back.