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.
This commit is contained in:
dotCooCoo committed 2026-12-02 00:41:23 -08:00
1 parent e8a6aa85c0
commit 8a3fe259e8
5 files changed
+40 -4

No files matched your search

+1 -1
View File
@@ -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",
+26
View File
@@ -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");
}
+5
View File
@@ -351,6 +351,11 @@ fn write_output(
then: impl FnOnce(&mut VectorcraftApp, String, Value) -> Result<Value, String> + 'static,
) -> Result<Value, String> {
let mut v = app.session.execute(id, &params).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)?;
+1 -1
View File
@@ -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",
+7 -2
View File
@@ -148,8 +148,9 @@ pub fn run(app: &mut VectorcraftApp, p: &Value) -> Result<Value, String> {
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<Value, String> {
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();