diff --git a/crates/engine/src/cmd/print_cmds.rs b/crates/engine/src/cmd/print_cmds.rs index 56bad41ec6..3a63a770be 100644 --- a/crates/engine/src/cmd/print_cmds.rs +++ b/crates/engine/src/cmd/print_cmds.rs @@ -930,6 +930,8 @@ fn driver_printable(p: &Value) -> Option { Some(json!([pg.printable.0, pg.printable.1, pg.printable.2, pg.printable.3])) } +/// Lays out and renders the print page as a PDF for `cmd` and, when the params ask for it, sends +/// it to the printer through `spooler`; returns the job summary (paper, image rect, spooler). fn render_and_spool(doc: &Document, p: &Value, cmd: &str, spooler: Option<&Spooler>) -> Result { let fit = p.get("scaleToFit").and_then(Value::as_bool).unwrap_or(false); let driver = (fit && native_route(p, spooler)).then(|| with_driver_printable(p)); @@ -1012,14 +1014,7 @@ fn render_and_spool(doc: &Document, p: &Value, cmd: &str, spooler: Option<&Spool /// that both load first (#5884, #8272). fn require_windows_pdf() -> Result<()> { #[cfg(target_os = "windows")] - { - if !system_dll_loads(WINDOWS_PDF_DLL) { - return Err(other( - "printing this page needs the Windows PDF component (Windows.Data.Pdf.dll), which is missing or cannot load. Save as PDF and print it from another app, or print an RGB image without page text or labels to print directly.", - )); - } - require_print_tickets()?; - } + require_windows_pdf_at(&system_root())?; Ok(()) } @@ -1027,7 +1022,25 @@ fn require_windows_pdf() -> Result<()> { /// API), which stripped Windows images can lack; the apps delay-load it (#8272). fn require_print_tickets() -> Result<()> { #[cfg(target_os = "windows")] - if !system_dll_loads(PRINT_TICKET_DLL) { + require_print_tickets_at(&system_root())?; + Ok(()) +} + +/// [`require_windows_pdf`] against the Windows directory `system_root` (tests pass a fixture). +#[cfg(target_os = "windows")] +fn require_windows_pdf_at(system_root: &std::path::Path) -> Result<()> { + if !dll_loads(&system_dll(system_root, WINDOWS_PDF_DLL)) { + return Err(other( + "printing this page needs the Windows PDF component (Windows.Data.Pdf.dll), which is missing or cannot load. Save as PDF and print it from another app, or print an RGB image without page text or labels to print directly.", + )); + } + require_print_tickets_at(system_root) +} + +/// [`require_print_tickets`] against the Windows directory `system_root` (tests pass a fixture). +#[cfg(target_os = "windows")] +fn require_print_tickets_at(system_root: &std::path::Path) -> Result<()> { + if !dll_loads(&system_dll(system_root, PRINT_TICKET_DLL)) { return Err(other( "printing this page needs the Windows Print Ticket component (prntvpt.dll), which is missing or cannot load. Save as PDF and print it from another app, or print an RGB image without page text or labels to print directly.", )); @@ -1040,12 +1053,13 @@ const WINDOWS_PDF_DLL: &str = "Windows.Data.Pdf.dll"; #[cfg(target_os = "windows")] const PRINT_TICKET_DLL: &str = "prntvpt.dll"; +/// The running Windows directory (`%SystemRoot%`), `C:\Windows` if the variable is unset. #[cfg(target_os = "windows")] -fn system_dll_loads(name: &str) -> bool { - let root = std::env::var_os("SystemRoot").unwrap_or_else(|| std::ffi::OsString::from(r"C:\Windows")); - dll_loads(&system_dll(std::path::Path::new(&root), name)) +fn system_root() -> std::path::PathBuf { + std::env::var_os("SystemRoot").map_or_else(|| std::path::PathBuf::from(r"C:\Windows"), std::path::PathBuf::from) } +/// Where the system DLL `name` lives under the Windows directory `system_root`. #[cfg(target_os = "windows")] fn system_dll(system_root: &std::path::Path, name: &str) -> std::path::PathBuf { system_root.join("System32").join(name) diff --git a/crates/engine/src/cmd/print_cmds/tests.rs b/crates/engine/src/cmd/print_cmds/tests.rs index 766215a10a..816815fc33 100644 --- a/crates/engine/src/cmd/print_cmds/tests.rs +++ b/crates/engine/src/cmd/print_cmds/tests.rs @@ -1183,11 +1183,13 @@ mod cups { } } +/// #5884, #8272: a Windows image without a loadable Windows.Data.Pdf.dll or prntvpt.dll reports +/// an error instead of aborting at the delay-load stub. The checks run against a fixture +/// `System32`, so they don't depend on what the host has installed. #[cfg(target_os = "windows")] #[test] fn windows_print_components_are_detected_by_loading_their_dlls() { - // #5884, #8272: a Windows image without a loadable Windows.Data.Pdf.dll or prntvpt.dll reports - // an error instead of aborting at the delay-load stub. + let system_dll_loads = |name| dll_loads(&system_dll(&system_root(), name)); let dir = tempfile::tempdir().unwrap(); std::fs::create_dir(dir.path().join("System32")).unwrap(); for name in [WINDOWS_PDF_DLL, PRINT_TICKET_DLL] { @@ -1197,12 +1199,23 @@ fn windows_print_components_are_detected_by_loading_their_dlls() { std::fs::write(&dll, b"MZ").unwrap(); assert!(!dll_loads(&dll), "{name}: not loadable"); } - // The real DLLs may be missing or unloadable on a stripped image; the checks follow the loader. - let tickets = system_dll_loads(PRINT_TICKET_DLL); - assert_eq!(require_print_tickets().is_ok(), tickets); - assert_eq!(require_windows_pdf().is_ok(), system_dll_loads(WINDOWS_PDF_DLL) && tickets); + // Both stubs fail to load, so both checks report the missing component. + assert!(require_print_tickets_at(dir.path()).is_err()); + assert!(require_windows_pdf_at(dir.path()).is_err()); + // With a loadable prntvpt.dll (the host's, copied in) only the PDF check still fails: it + // checks Windows.Data.Pdf.dll itself, not just the Print Ticket API. + let tickets = system_dll(dir.path(), PRINT_TICKET_DLL); + if system_dll_loads(PRINT_TICKET_DLL) && std::fs::copy(system_dll(&system_root(), PRINT_TICKET_DLL), &tickets).is_ok() { + assert_eq!(require_print_tickets_at(dir.path()).is_ok(), dll_loads(&tickets)); + assert!(require_windows_pdf_at(dir.path()).is_err(), "Windows.Data.Pdf.dll is still a stub"); + } + // The wrappers follow the loader on the real Windows directory (a stripped image may lack either). + let host_tickets = system_dll_loads(PRINT_TICKET_DLL); + assert_eq!(require_print_tickets().is_ok(), host_tickets); + assert_eq!(require_windows_pdf().is_ok(), system_dll_loads(WINDOWS_PDF_DLL) && host_tickets); } +/// Off Windows there are no Windows print components to check, so both checks pass. #[cfg(not(target_os = "windows"))] #[test] fn the_windows_pdf_check_is_a_no_op_elsewhere() {