Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Windows: start without Windows.Data.Pdf.dll; delay-load it for printing (#5884)

Higgs32584cf3d26881d77
diff --git a/apps/photocraft-cli/build.rs b/apps/photocraft-cli/build.rs
index c757a4a7c..3951c46b8 100644
--- a/apps/photocraft-cli/build.rs
+++ b/apps/photocraft-cli/build.rs
@@ -12,6 +12,14 @@ fn main() -> std::io::Result<()> {
if std::env::var("CARGO_CFG_TARGET_OS").as_deref() != Ok("windows") {
return Ok(());
}
+ // Printing to PDF (winprint's `PdfCreateRenderer`) imports Windows.Data.Pdf.dll, which
+ // stripped Windows images (NTLite, some N/LTSC builds) lack. A load-time import stops the exe
+ // from starting at all ("windows.data.pdf.dll was not found", #5884), so load it on first use;
+ // the print command checks for it first and reports an error instead.
+ if std::env::var("CARGO_CFG_TARGET_ENV").as_deref() == Ok("msvc") {
+ println!("cargo:rustc-link-arg-bins=/DELAYLOAD:windows.data.pdf.dll");
+ println!("cargo:rustc-link-arg-bins=delayimp.lib");
+ }
let mut res = winresource::WindowsResource::new();
// FileVersion / ProductVersion strings and the numeric FILEVERSION / PRODUCTVERSION are left
// to `WindowsResource::new`, which reads the Cargo package version. A pre-release such as
diff --git a/apps/photocraft/build.rs b/apps/photocraft/build.rs
index cbba62258..48abc9f1a 100644
--- a/apps/photocraft/build.rs
+++ b/apps/photocraft/build.rs
@@ -11,6 +11,14 @@ fn main() {
if std::env::var("CARGO_CFG_TARGET_OS").as_deref() != Ok("windows") {
return;
}
+ // Printing to PDF (winprint's `PdfCreateRenderer`) imports Windows.Data.Pdf.dll, which
+ // stripped Windows images (NTLite, some N/LTSC builds) lack. A load-time import stops the exe
+ // from starting at all ("windows.data.pdf.dll was not found", #5884), so load it on first use;
+ // the print command checks for it first and reports an error instead.
+ if std::env::var("CARGO_CFG_TARGET_ENV").as_deref() == Ok("msvc") {
+ println!("cargo:rustc-link-arg-bins=/DELAYLOAD:windows.data.pdf.dll");
+ println!("cargo:rustc-link-arg-bins=delayimp.lib");
+ }
let mut res = winresource::WindowsResource::new();
res.set_icon("../../assets/app-icon/photocraft.ico")
.set("ProductName", "PhotoCraft")
diff --git a/crates/engine/src/print_cmds.rs b/crates/engine/src/print_cmds.rs
index 9a0af4a05..69f450553 100644
--- a/crates/engine/src/print_cmds.rs
+++ b/crates/engine/src/print_cmds.rs
@@ -427,6 +427,7 @@ fn render_and_spool(doc: &Document, p: &Value, cmd: &str) -> Result<Value> {
let mut spool = Value::Null;
if send && !dry {
require_printing_enabled()?;
+ require_windows_pdf()?;
spool = json!(craft_print::submit(&pdf, &job).map_err(other)?);
sent = true;
}
@@ -435,6 +436,25 @@ fn render_and_spool(doc: &Document, p: &Value, cmd: &str) -> Result<Value> {
)
}
+/// Windows prints a PDF through Windows.Data.Pdf.dll, which the apps delay-load (see their
+/// `build.rs`) so a Windows image without it still starts. Calling into it when it is missing
+/// would abort the process, so check that it is installed first (#5884).
+fn require_windows_pdf() -> Result<()> {
+ if !cfg!(windows) {
+ return Ok(());
+ }
+ let root = std::env::var_os("SystemRoot").unwrap_or_else(|| std::ffi::OsString::from(r"C:\Windows"));
+ if windows_pdf_installed(std::path::Path::new(&root)) {
+ Ok(())
+ } else {
+ Err(other("printing this page needs the Windows PDF component (Windows.Data.Pdf.dll), which is not installed. Save as PDF and print it from another app, or clear Print Settings to print directly."))
+ }
+}
+
+fn windows_pdf_installed(system_root: &std::path::Path) -> bool {
+ system_root.join("System32").join("Windows.Data.Pdf.dll").is_file()
+}
+
/// Keep CMYK output profiles and page text on the existing PDF composition path. GDI currently
/// draws RGB/gray and vector marks only; it must never silently omit requested page content.
fn native_rgb_compatible(page: &PrintPage, handling: &str) -> bool {
diff --git a/crates/engine/src/print_cmds/tests.rs b/crates/engine/src/print_cmds/tests.rs
index d012f6de4..677fdec6b 100644
--- a/crates/engine/src/print_cmds/tests.rs
+++ b/crates/engine/src/print_cmds/tests.rs
@@ -434,3 +434,13 @@ fn printers_lists_the_spooler_destinations_without_a_document() {
None => assert!(list.iter().all(|p| p["default"] == false)),
}
}
+
+#[test]
+fn windows_pdf_component_is_detected_by_its_dll() {
+ // #5884: a Windows image without Windows.Data.Pdf.dll reports an error instead of aborting.
+ let dir = tempfile::tempdir().unwrap();
+ assert!(!windows_pdf_installed(dir.path()));
+ std::fs::create_dir(dir.path().join("System32")).unwrap();
+ std::fs::write(dir.path().join("System32").join("Windows.Data.Pdf.dll"), b"MZ").unwrap();
+ assert!(windows_pdf_installed(dir.path()));
+}