Skip to content

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

Merge PR #2648: fix(xfa): skip PDF fallback scripts while XFA layout is active

aiongg4fedfe7e8c9d
diff --cc crates/engine/src/js.rs
index a59743dbb,ff3000f27..c3a6a8ef7
--- a/crates/engine/src/js.rs
+++ b/crates/engine/src/js.rs
@@@ -129,23 -129,15 +129,29 @@@ pub struct JsRunner
}
impl JsRunner {
+ /// A runner for `cos`: what scripts read about the document (pages, info, whether it is
+ /// an XFA form) and its document-level scripts, read once.
pub fn new(cos: &pdfcraft_cos::Document, file_name: &str) -> JsRunner {
- let doc = pdfcraft_js::DocInfo { file_name: file_name.to_string(), num_pages: pdfcraft_model::pages(cos).len(), page: 0, info: info(cos) };
+ let doc = pdfcraft_js::DocInfo {
+ file_name: file_name.to_string(),
+ num_pages: pdfcraft_model::pages(cos).len(),
+ page: 0,
+ info: info(cos),
+ xfa: pdfcraft_xfa::xfa_shape(cos).is_some(),
+ };
+ // Once XFA owns the form, the PDF container's scripts belong to its fallback view.
+ // Keep them in the file and in Document JavaScripts, but do not initialize an
+ // AcroForm context with them: a viewer-compatibility alert would be misleading.
+ // Packets alone are not enough: static XFA and an unlaid-out shell keep their scripts.
+ let xfa_active = pdfcraft_xfa::xfa_shape(cos).is_some() && pdfcraft_xfa::existing_layout(cos).is_some();
+ let doc_scripts = if xfa_active { Vec::new() } else { pdfcraft_forms::document_scripts(cos) };
- JsRunner { doc, doc_scripts, output: JsOutput::default(), cos: cos.clone(), displays: std::collections::HashMap::new() }
+ JsRunner {
+ doc,
- doc_scripts: pdfcraft_forms::document_scripts(cos),
++ doc_scripts,
+ output: JsOutput::default(),
+ cos: cos.clone(),
+ displays: std::collections::HashMap::new(),
+ }
}
}
diff --cc crates/engine/src/tests.rs
index ed4e6efa1,d1fa6dfed..b62671181
--- a/crates/engine/src/tests.rs
+++ b/crates/engine/src/tests.rs
@@@ -4171,20 -3911,78 +4171,92 @@@ fn form_javascript_validates_calculates
assert!(s.run_javascript(id, "1", None).is_err());
}
+/// XFA forms check `xfa_installed` and `xfa_version` in a document script that runs before
+/// each of their scripts, and alert when either is missing.
+#[test]
+fn scripts_in_an_xfa_form_see_the_xfa_plugin_installed() {
+ let check = "typeof xfa_installed + ' ' + typeof xfa_version + ' ' + xfa_version";
+ let mut s = Session::new().with_clock(|| 1_700_000_000);
+ let xfa = s.open("form.pdf", None, Arc::new(pdfcraft_xfa::fixtures::shell(&pdfcraft_xfa::fixtures::template(3))), None).unwrap();
+ let o = s.run_javascript(xfa, check, None).unwrap();
+ assert_eq!((o.error, o.result.as_deref()), (None, Some("boolean number 3.5")));
+ let plain = s.open("order.pdf", None, Arc::new(scripted_form()), None).unwrap();
+ let o = s.run_javascript(plain, check, None).unwrap();
+ assert_eq!((o.error, o.result.as_deref()), (None, Some("undefined number 3.5")));
+}
+
+ #[test]
+ fn active_xfa_does_not_run_pdf_container_scripts_but_keeps_requested_alerts() {
+ let template = pdfcraft_xfa::fixtures::scripted_template().replace(
+ r#"xfa.host.messageBox("Hello " + qty.rawValue);"#,
+ r#"xfa.host.messageBox("Hello " + qty.rawValue); xfa.host.print(1, "0", "0", 0, 0, 0, 0, 0);"#,
+ );
+ let mut cos = pdfcraft_cos::Document::open(Arc::new(pdfcraft_xfa::fixtures::shell(&template))).unwrap();
+ let fallback = "app.alert('This PDF form requires a newer version of Adobe Acrobat');";
+ pdfcraft_forms::set_document_script(&mut cos, "viewerFallback", Some(fallback)).unwrap();
+ let bytes = Arc::new(pdfcraft_cos::write_full(&cos, &Default::default()).unwrap());
+ let mut s = Session::new();
+ let id = s.open("xfa.pdf", None, bytes, None).unwrap();
+ assert!(s.get(id).unwrap().xfa.is_some());
+ assert!(s.take_js_output(id).alerts.is_empty());
+ let out = s.run_javascript(id, "app.alert('Console message');", None).unwrap();
+ assert!(out.error.is_none(), "{out:?}");
+ assert_eq!(out.alerts, ["Console message"]);
+ assert_eq!(s.get(id).unwrap().document_scripts(), [("viewerFallback".into(), fallback.into())]);
+ let out = s.run_javascript(id, "", Some("hello")).unwrap();
+ assert!(out.error.is_none(), "{out:?}");
+ assert_eq!(out.alerts, ["Hello 2"]);
+ assert_eq!(out.requests, [js::Request::Print]);
+ // The same context rule applies to format/validate/calculate through JsRunner.
+ s.apply(id, Edit::SetFieldScript { name: "qty".into(), event: "validate".into(), script: Some("app.alert('Field message');".into()) }).unwrap();
+ s.take_js_output(id);
+ s.apply(id, Edit::SetFieldValue { name: "qty".into(), value: FieldValue::Text("3".into()) }).unwrap();
+ assert_eq!(s.take_js_output(id).alerts, ["Field message"]);
+ // Saved XFA keeps its container scripts for inspection, but does not execute them.
+ let saved = s.get(id).unwrap().bytes.clone();
+ let reopened = s.open("saved.pdf", None, saved, None).unwrap();
+ assert!(s.run_javascript(reopened, "", None).unwrap().alerts.is_empty());
+ // Making the form static restores the ordinary PDF context; undo restores XFA's.
+ s.apply(id, Edit::XfaToStatic).unwrap();
+ assert_eq!(s.run_javascript(id, "", None).unwrap().alerts.len(), 1);
+ s.undo(id).unwrap();
+ assert!(s.run_javascript(id, "", None).unwrap().alerts.is_empty());
+ }
+
+ #[test]
+ fn active_xfa_without_template_scripts_does_not_initialize_pdf_fallback_scripts() {
+ let template = r#"<template xmlns="http://www.xfa.org/schema/xfa-template/3.3/">
+ <subform name="form" layout="tb"><pageSet><pageArea name="page">
+ <contentArea x="36pt" y="36pt" w="540pt" h="720pt"/><medium short="612pt" long="792pt"/>
+ </pageArea></pageSet><field name="text" w="200pt" h="30pt"><ui><textEdit/></ui></field></subform></template>"#;
+ let mut s = Session::new();
+ let id = s.open("no-scripts.pdf", None, Arc::new(pdfcraft_xfa::fixtures::shell(template)), None).unwrap();
+ assert!(s.get(id).unwrap().xfa.is_some());
+ assert!(s.get(id).unwrap().xfa_template.is_none());
+ s.apply(id, Edit::SetDocumentScript { name: "fallback".into(), script: Some("throw new Error('PDF fallback ran');".into()) }).unwrap();
+ let out = s.run_javascript(id, "app.alert('Console message');", None).unwrap();
+ assert!(out.error.is_none(), "{out:?}");
+ assert_eq!(out.alerts, ["Console message"]);
+ }
+
+ #[test]
+ fn inactive_xfa_and_ordinary_pdfs_keep_document_helpers_and_alerts() {
+ for bytes in [
+ fixture(1),
+ pdfcraft_xfa::fixtures::static_shell("<form1><page1><name>Test</name></page1></form1>"),
+ pdfcraft_xfa::fixtures::shell(&pdfcraft_xfa::fixtures::scripted_template()),
+ ] {
+ let mut cos = pdfcraft_cos::Document::open(Arc::new(bytes)).unwrap();
+ pdfcraft_forms::set_document_script(&mut cos, "helper", Some("function twice(n) { return n * 2; } app.alert('PDF message');")).unwrap();
+ // The raw dynamic shell has not been laid out: XFA's presence alone is not enough.
+ let runner = js::JsRunner::new(&cos, "control.pdf");
+ let out = pdfcraft_js::run("twice(3)", &pdfcraft_js::Event::doc("Console"), &runner.doc, &[], &runner.doc_scripts, Default::default());
+ assert!(out.error.is_none(), "{out:?}");
+ assert_eq!(out.result.as_deref(), Some("6"));
+ assert_eq!(out.alerts, ["PDF message"]);
+ }
+ }
+
#[test]
fn edit_history_does_not_replay_field_script_side_effects() {
let mut s = Session::new().with_clock(|| 1_700_000_000);