From 8a3fe259e8f4f55a0bc8d204c1a539bfa5e7ba02 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 2 Dec 2026 00:41:23 -0800 Subject: [PATCH] Print: file.print in the app passes maxPages on to the engine The app's file.print forwarded preset, settings, format, level and flattenerPreset to the engine but dropped maxPages, so a script that set a page limit through Document.print (added in #9087) still printed or saved every page. The app now forwards it, returns the engine's {pages, format} without printing or saving when the job is past the limit, and passes an explicit null on so the engine rejects it instead of treating it as no limit. The command text in both catalogs lists the parameter. --- crates/mcp/src/ui_catalog.rs | 2 +- crates/ui-egui/src/dialogs/tests_print.rs | 26 +++++++++++++++++++++++ crates/ui-egui/src/io/export.rs | 5 +++++ crates/ui-egui/src/menus/ui_commands.rs | 2 +- crates/ui-egui/src/print.rs | 9 ++++++-- 5 files changed, 40 insertions(+), 4 deletions(-) diff --git a/crates/mcp/src/ui_catalog.rs b/crates/mcp/src/ui_catalog.rs index 6852f317b3..e9e85a3e0d 100644 --- a/crates/mcp/src/ui_catalog.rs +++ b/crates/mcp/src/ui_catalog.rs @@ -1991,7 +1991,7 @@ pub const UI_COMMANDS: &[(&str, &str, &str, &str)] = &[ "file.print", "Print…", "Cmd+P", - "{} open the Print dialog (dialog `print`: the print.setup settings, preset (setting it loads that print preset; Save Preset… saves them as one), printer, toFile; Print keeps the settings with the document and prints, Done (discard: true) only keeps them); with params {preset? (a print preset's name: its settings instead of the document's), settings? (over the document's or the preset's), printer? (print.printers name; \"\" the default), toFile?, path?, format?: pdf|postscript, level?, flattenerPreset? (as engine file.print)} print without it: the print-ready PDF (engine file.print) goes to the printer → {pages, printer, printed: true, warnings}; with toFile, a path or no printing here (no print service) it is saved as a PDF at path (else a picked file, the web downloads it) → {path, pages, printed: false, warnings}; a PostScript file (format postscript, or a .ps path) is always saved that way", + "{} open the Print dialog (dialog `print`: the print.setup settings, preset (setting it loads that print preset; Save Preset… saves them as one), printer, toFile; Print keeps the settings with the document and prints, Done (discard: true) only keeps them); with params {preset? (a print preset's name: its settings instead of the document's), settings? (over the document's or the preset's), printer? (print.printers name; \"\" the default), toFile?, path?, format?: pdf|postscript, level?, flattenerPreset?, maxPages? (as engine file.print)} print without it: the print-ready PDF (engine file.print) goes to the printer → {pages, printer, printed: true, warnings}; with toFile, a path or no printing here (no print service) it is saved as a PDF at path (else a picked file, the web downloads it) → {path, pages, printed: false, warnings}; a PostScript file (format postscript, or a .ps path) is always saved that way; a job of more than maxPages pages is neither printed nor saved → {pages, format}", ), ( "print.printers", diff --git a/crates/ui-egui/src/dialogs/tests_print.rs b/crates/ui-egui/src/dialogs/tests_print.rs index 183d6f1056..137b9dffb7 100644 --- a/crates/ui-egui/src/dialogs/tests_print.rs +++ b/crates/ui-egui/src/dialogs/tests_print.rs @@ -620,3 +620,29 @@ fn every_number_and_length_field_is_named() { } } } + +#[test] +fn print_past_max_pages_neither_prints_nor_saves() { + // Three artboards × 2 copies is 6 pages: a limit of 5 sends no job. + let (mut app, jobs) = app(); + let r = app.run("file.print", json!({"settings": {"copies": 2}, "maxPages": 5})).unwrap(); + assert_eq!(r, json!({"pages": 6, "format": "pdf"})); + assert!(jobs.borrow().is_empty(), "nothing is sent"); + // Within the limit the job prints as before. + let r = app.run("file.print", json!({"settings": {"copies": 2}, "maxPages": 6})).unwrap(); + assert_eq!((r["pages"].clone(), r["printed"].clone()), (json!(6), json!(true))); + assert_eq!(jobs.borrow().len(), 1); + // Saved to a path, a job past the limit writes nothing. + let written: Written = Rc::default(); + let w = written.clone(); + app.services.write = Some(Box::new(move |p: &str, b: &[u8]| { + w.borrow_mut().push((p.to_string(), b.to_vec())); + Ok(()) + })); + let r = app.run("file.print", json!({"path": "/tmp/job.pdf", "settings": {"copies": 2}, "maxPages": 5})).unwrap(); + assert_eq!(r, json!({"pages": 6, "format": "pdf"})); + assert!(written.borrow().is_empty(), "nothing is written"); + // A null limit is an error, not "no limit". + assert!(app.run("file.print", json!({"maxPages": null})).is_err()); + assert_eq!(jobs.borrow().len(), 1, "nothing more is sent"); +} diff --git a/crates/ui-egui/src/io/export.rs b/crates/ui-egui/src/io/export.rs index c82c4c76de..7f95ad3249 100644 --- a/crates/ui-egui/src/io/export.rs +++ b/crates/ui-egui/src/io/export.rs @@ -351,6 +351,11 @@ fn write_output( then: impl FnOnce(&mut VectorcraftApp, String, Value) -> Result + 'static, ) -> Result { let mut v = app.session.execute(id, ¶ms).map_err(|e| e.to_string())?; + // `file.print` past its `maxPages` composes nothing: its `{pages, format}` is the answer and no + // file is written. + if params.get("maxPages").is_some() && v.get("dataBase64").is_none() { + return Ok(v); + } let bytes = take_output(&mut v)?; let write = move |app: &mut VectorcraftApp, path: String| { write_reporting(app, &path, &bytes)?; diff --git a/crates/ui-egui/src/menus/ui_commands.rs b/crates/ui-egui/src/menus/ui_commands.rs index b4cf91497d..084c74d168 100644 --- a/crates/ui-egui/src/menus/ui_commands.rs +++ b/crates/ui-egui/src/menus/ui_commands.rs @@ -1992,7 +1992,7 @@ pub const UI_COMMANDS: &[(&str, &str, &str, &str)] = &[ "file.print", "Print…", "Cmd+P", - "{} open the Print dialog (dialog `print`: the print.setup settings, preset (setting it loads that print preset; Save Preset… saves them as one), printer, toFile; Print keeps the settings with the document and prints, Done (discard: true) only keeps them); with params {preset? (a print preset's name: its settings instead of the document's), settings? (over the document's or the preset's), printer? (print.printers name; \"\" the default), toFile?, path?, format?: pdf|postscript, level?, flattenerPreset? (as engine file.print)} print without it: the print-ready PDF (engine file.print) goes to the printer → {pages, printer, printed: true, warnings}; with toFile, a path or no printing here (no print service) it is saved as a PDF at path (else a picked file, the web downloads it) → {path, pages, printed: false, warnings}; a PostScript file (format postscript, or a .ps path) is always saved that way", + "{} open the Print dialog (dialog `print`: the print.setup settings, preset (setting it loads that print preset; Save Preset… saves them as one), printer, toFile; Print keeps the settings with the document and prints, Done (discard: true) only keeps them); with params {preset? (a print preset's name: its settings instead of the document's), settings? (over the document's or the preset's), printer? (print.printers name; \"\" the default), toFile?, path?, format?: pdf|postscript, level?, flattenerPreset?, maxPages? (as engine file.print)} print without it: the print-ready PDF (engine file.print) goes to the printer → {pages, printer, printed: true, warnings}; with toFile, a path or no printing here (no print service) it is saved as a PDF at path (else a picked file, the web downloads it) → {path, pages, printed: false, warnings}; a PostScript file (format postscript, or a .ps path) is always saved that way; a job of more than maxPages pages is neither printed nor saved → {pages, format}", ), ( "print.printers", diff --git a/crates/ui-egui/src/print.rs b/crates/ui-egui/src/print.rs index 61860ee915..e5bc13c6e0 100644 --- a/crates/ui-egui/src/print.rs +++ b/crates/ui-egui/src/print.rs @@ -148,8 +148,9 @@ pub fn run(app: &mut VectorcraftApp, p: &Value) -> Result { let postscript = s("format").as_deref() == Some("postscript") || s("path").is_some_and(|path| fileio::extension(&path) == "ps"); let to_file = p.get("toFile").and_then(Value::as_bool).unwrap_or(false) || s("path").is_some() || postscript; let mut q = json!({}); - for k in ["preset", "settings", "format", "level", "flattenerPreset"] { - if let Some(v) = p.get(k).filter(|v| !v.is_null()) { + for k in ["preset", "settings", "format", "level", "flattenerPreset", "maxPages"] { + // An explicit null `maxPages` goes on, so the engine rejects it instead of printing every page. + if let Some(v) = p.get(k).filter(|v| !v.is_null() || k == "maxPages") { q[k] = v.clone(); } } @@ -175,6 +176,10 @@ pub fn run(app: &mut VectorcraftApp, p: &Value) -> Result { q["forPrinter"] = json!(true); } let mut v = app.session.execute("file.print", &q).map_err(|e| e.to_string())?; + // A job past `maxPages` is not composed: the reply is `{pages, format}` and nothing is sent. + if q.get("maxPages").is_some() && v.get("dataBase64").is_none() { + return Ok(v); + } let data = v.as_object_mut().and_then(|o| o.remove("dataBase64")); let pdf = data.as_ref().and_then(Value::as_str).and_then(vectorcraft_format::base64_decode).ok_or("the print job has no data")?; let title = app.session.active().map(|d| d.title()).unwrap_or_default();