diff --git a/Cargo.lock b/Cargo.lock index e50a11fdc..191ee67f3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -38,6 +38,7 @@ dependencies = [ "js-sys", "libloading", "log", + "lopdf", "lyon_tessellation", "meshopt", "naga 27.0.3", diff --git a/Cargo.toml b/Cargo.toml index b8d1a54fd..f65b42ce0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -93,6 +93,8 @@ i18n-embed = { version = "0.16", features = ["desktop-requester"] } rayon = "1" open = "5" printpdf = { version = "0.9.1", default-features = false, features = ["images"] } +# The lopdf printpdf builds on: used to Flate-compress the exported PDF streams. +lopdf = { version = "0.39.0", default-features = false, features = ["time"] } ureq = { version = "3", default-features = false, features = ["rustls", "platform-verifier", "win-system-proxy"] } [target.'cfg(target_arch = "wasm32")'.dependencies] diff --git a/src/io/pdf_export.rs b/src/io/pdf_export.rs index 774ec9afa..4b505bc93 100644 --- a/src/io/pdf_export.rs +++ b/src/io/pdf_export.rs @@ -424,7 +424,16 @@ fn build_pdf_pages(pages: &[PdfPageInput], plot_style: Option<&PlotStyleTable>) .map_err(|error| format!("Page {}: {error}", index + 1))?; } let mut warnings = Vec::new(); - Ok(doc.save(&PdfSaveOptions::default(), &mut warnings)) + // printpdf 0.9's `optimize` is a no-op (its `doc.compress()` is commented + // out), so every page's content stream — megabytes of vector operators + // for a CAD sheet — went out uncompressed: a 16-sheet set weighed 280 MB. + // Serialise through lopdf ourselves and Flate-compress the streams. + let mut lo = printpdf::to_lopdf_doc(&doc, &PdfSaveOptions::default(), &mut warnings); + lo.compress(); + let mut bytes = Vec::new(); + lo.save_to(&mut bytes) + .map_err(|error| format!("PDF serialisation failed: {error}"))?; + Ok(bytes) } #[cfg(not(target_arch = "wasm32"))]