Skip to content

PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust

fix(ui): the PDF/A dialog shows a declared PDF/UA (#1309) (#1438)

Marco Arnulfo0372243ef2f0
diff --git a/crates/ui-egui/src/standards_ui.rs b/crates/ui-egui/src/standards_ui.rs
index 55f643499..1d409f200 100644
--- a/crates/ui-egui/src/standards_ui.rs
+++ b/crates/ui-egui/src/standards_ui.rs
@@ -50,7 +50,9 @@ pub(crate) fn body(ui: &mut egui::Ui, app: &mut PdfCraftApp, t: &Tokens) -> bool
ui.label(egui::RichText::new("PDF/A").font(theme::semibold(18.0)));
ui.add_space(6.0);
let declared = app.active_ids().and_then(|(_, id)| app.session.get(id)).map(|d| d.standards()).unwrap_or_default();
- let shown = declared.pdfa.as_ref().map_or(tl!("none").to_string(), |(p, c)| format!("PDF/A-{p}{}", c.to_lowercase()));
+ let mut standards: Vec<String> = declared.pdfa.iter().map(|(p, c)| format!("PDF/A-{p}{}", c.to_lowercase())).collect();
+ standards.extend(declared.pdfua.map(|p| format!("PDF/UA-{p}")));
+ let shown = if standards.is_empty() { tl!("none").to_string() } else { standards.join(", ") };
ui.label(crate::i18n::fmt(tl!("Declared conformance: {shown}"), &[("shown", &shown)]));
if !declared.output_intents.is_empty() {
ui.label(
diff --git a/crates/ui-egui/tests/compare.rs b/crates/ui-egui/tests/compare.rs
index e2536c6d4..089d3b882 100644
--- a/crates/ui-egui/tests/compare.rs
+++ b/crates/ui-egui/tests/compare.rs
@@ -90,6 +90,54 @@ fn pdfa_dialog_verifies_and_converts() {
assert_eq!(h.state().session.get(id).unwrap().can_undo(), Some("Save as PDF/A-2b"));
}
+/// A one-page PDF whose XMP declares the given `pdfaid`/`pdfuaid` attributes.
+fn declaring(ids: &str) -> Vec<u8> {
+ let xmp = format!(
+ "<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"><rdf:Description rdf:about=\"\" \
+ xmlns:pdfaid=\"http://www.aiim.org/pdfa/ns/id/\" xmlns:pdfuaid=\"http://www.aiim.org/pdfua/ns/id/\" {ids}/></rdf:RDF>"
+ );
+ let objects = [
+ "<< /Type /Catalog /Pages 2 0 R /Metadata 4 0 R >>".to_string(),
+ "<< /Type /Pages /Kids [3 0 R] /Count 1 >>".to_string(),
+ "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 200 300] /Resources << >> >>".to_string(),
+ format!("<< /Type /Metadata /Subtype /XML /Length {} >>\nstream\n{xmp}\nendstream", xmp.len()),
+ ];
+ let mut pdf = b"%PDF-1.7\n".to_vec();
+ let mut offsets = Vec::new();
+ for (i, o) in objects.iter().enumerate() {
+ offsets.push(pdf.len());
+ pdf.extend_from_slice(format!("{} 0 obj\n{o}\nendobj\n", i + 1).as_bytes());
+ }
+ let xref = pdf.len();
+ pdf.extend_from_slice(b"xref\n0 5\n0000000000 65535 f \n");
+ for o in offsets {
+ pdf.extend_from_slice(format!("{o:010} 00000 n \n").as_bytes());
+ }
+ pdf.extend_from_slice(format!("trailer\n<< /Size 5 /Root 1 0 R >>\nstartxref\n{xref}\n%%EOF\n").as_bytes());
+ pdf
+}
+
+#[test]
+fn pdfa_dialog_shows_a_declared_pdfua() {
+ // The dialog read only the PDF/A identification, so a PDF/UA-1 document showed "none" (#1309).
+ for (ids, shown) in [
+ ("pdfuaid:part=\"1\"", "Declared conformance: PDF/UA-1"),
+ ("pdfaid:part=\"2\" pdfaid:conformance=\"B\" pdfuaid:part=\"1\"", "Declared conformance: PDF/A-2b, PDF/UA-1"),
+ ] {
+ let doc = declaring(ids);
+ let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |_cc| {
+ let mut app = PdfCraftApp::new();
+ app.set_option("language", "en").unwrap();
+ app.open_bytes("ua.pdf", None, doc.clone()).unwrap();
+ app
+ });
+ h.run_steps(4);
+ assert!(h.state_mut().execute("standards.pdfa"));
+ h.run_steps(2);
+ h.get_by_label(shown);
+ }
+}
+
#[test]
fn export_to_word_html_and_rtf() {
let doc = Session::new().create_from_text("t", "Exported words").unwrap().to_vec();