PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust
export: zero-pad the page number in Export All Images file names (#2208) (#2295)
snipeTR5288946b5464
diff --git a/crates/automation/src/lib.rs b/crates/automation/src/lib.rsindex 751c1801d..a832d0685 100644--- a/crates/automation/src/lib.rs+++ b/crates/automation/src/lib.rs@@ -1218,7 +1218,7 @@ impl Automation {let out = pdfcraft_engine::export::extract_images(&doc.export_source(), &pages, min).map_err(failed)?;let mut files = Vec::new();for (k, img) in out.images.iter().enumerate() {- let path = child(&folder, &pdfcraft_engine::export::image_file_name(&stem, img, k + 1));+ let path = child(&folder, &pdfcraft_engine::export::image_file_name(&stem, img, k + 1, doc.info.pages.len()));write_atomic(&path, &img.data)?;files.push(json!({ "path": path.to_string_lossy(), "page": img.page + 1, "width": img.width, "height": img.height }));}diff --git a/crates/automation/src/tools.rs b/crates/automation/src/tools.rsindex ba79a7ade..78476167f 100644--- a/crates/automation/src/tools.rs+++ b/crates/automation/src/tools.rs@@ -1123,7 +1123,7 @@ pub fn tools() -> Vec<ToolDef> {.ro().cmd("ocr.recognize").with(schema(json!({}), &[])),- t("doc_export_all_images", "Export all images", "Write the images that pages use into a folder (`<name>_Page_<n>_Image_<k>.jpg|png`), each once: JPEG images unchanged, others as PNG with their soft mask as alpha. min_size skips images with fewer pixels on their shorter side. Images that can't be decoded yet (JPEG 2000, JBIG2, CCITT, separations) are listed under skipped. Includes unsaved edits.")+ t("doc_export_all_images", "Export all images", "Write the images that pages use into a folder (`<name>_Page_<n>_Image_<k>.jpg|png`, n zero-padded to the page count's digits), each once: JPEG images unchanged, others as PNG with their soft mask as alpha. min_size skips images with fewer pixels on their shorter side. Images that can't be decoded yet (JPEG 2000, JBIG2, CCITT, separations) are listed under skipped. Includes unsaved edits.").cmd("export.all_images").with(schema(json!({diff --git a/crates/engine/src/export.rs b/crates/engine/src/export.rsindex a6af0cdf8..579a483ef 100644--- a/crates/engine/src/export.rs+++ b/crates/engine/src/export.rs@@ -46,8 +46,11 @@ pub fn extract_images(src: &ExportSource, pages: &[usize], min_side: u32) -> Res}/// The file name for the `index`-th (1-based) exported image: `<stem>_Page_<n>_Image_<index>.<ext>`.-pub fn image_file_name(stem: &str, image: &pdfcraft_create::ExtractedImage, index: usize) -> String {- format!("{stem}_Page_{}_Image_{index:04}.{}", image.page + 1, image.extension)+/// The page number has leading zeros for as many digits as the document's `page_count` has, so the+/// files sort in page order (#2208): page 7 of 120 is `Page_007`.+pub fn image_file_name(stem: &str, image: &pdfcraft_create::ExtractedImage, index: usize, page_count: usize) -> String {+ let width = page_count.max(1).to_string().len();+ format!("{stem}_Page_{:0width$}_Image_{index:04}.{}", image.page + 1, image.extension)}impl Exporter {@@ -212,3 +215,29 @@ pub fn encode_tiff(width: u32, height: u32, premultiplied: &[u8]) -> Result<Vec<image::codecs::tiff::TiffEncoder::new(&mut out).write_image(&rgb, width, height, image::ExtendedColorType::Rgb8).map_err(|e| e.to_string())?;Ok(out.into_inner())}++#[cfg(test)]+mod image_name_tests {+ use super::image_file_name;++ fn image(page: usize) -> pdfcraft_create::ExtractedImage {+ pdfcraft_create::ExtractedImage {+ page,+ object: pdfcraft_cos::ObjRef { num: 1, generation: 0 },+ width: 1,+ height: 1,+ extension: "png",+ data: Vec::new(),+ }+ }++ #[test]+ fn page_number_is_padded_to_the_page_count() {+ assert_eq!(image_file_name("doc", &image(0), 1, 1), "doc_Page_1_Image_0001.png");+ assert_eq!(image_file_name("doc", &image(0), 1, 9), "doc_Page_1_Image_0001.png");+ assert_eq!(image_file_name("doc", &image(6), 2, 20), "doc_Page_07_Image_0002.png");+ assert_eq!(image_file_name("doc", &image(6), 3, 120), "doc_Page_007_Image_0003.png");+ assert_eq!(image_file_name("doc", &image(119), 4, 120), "doc_Page_120_Image_0004.png");+ assert_eq!(image_file_name("doc", &image(0), 1, 0), "doc_Page_1_Image_0001.png");+ }+}diff --git a/crates/ui-egui/src/export_ui.rs b/crates/ui-egui/src/export_ui.rsindex bc6f353a1..fae075e30 100644--- a/crates/ui-egui/src/export_ui.rs+++ b/crates/ui-egui/src/export_ui.rs@@ -153,7 +153,7 @@ fn run(Err(e) => return crate::i18n::fmt(tl!("Export stopped: {e}"), &[("e", &e.to_string())]),};for (k, img) in out.images.iter().enumerate() {- if let Err(e) = sink(&pdfcraft_engine::export::image_file_name(&stem, img, k + 1), img.data.clone()) {+ if let Err(e) = sink(&pdfcraft_engine::export::image_file_name(&stem, img, k + 1, src.pages), img.data.clone()) {return crate::i18n::fmt(tl!("Export stopped: {e}"), &[("e", &e.to_string())]);}}