diff --git a/crates/mcp/src/ui_catalog.rs b/crates/mcp/src/ui_catalog.rs index 8dfdfd28e3..9dedb0dfc0 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 d2ebf74122..fbc5eaf43b 100644 --- a/crates/ui-egui/src/dialogs/tests_print.rs +++ b/crates/ui-egui/src/dialogs/tests_print.rs @@ -1036,3 +1036,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 3e4a834cb8..d5fe92f798 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 0451cb5b5c..cb7bae6315 100644 --- a/crates/ui-egui/src/print.rs +++ b/crates/ui-egui/src/print.rs @@ -239,8 +239,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(); } } @@ -266,6 +267,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();