PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust
feat(cli): page lists take ranges, and a missing page is named as typed (#1923) (#1950)
mtlcl410355907703e31
diff --git a/README.md b/README.mdindex c439879f0..f563ee2fe 100644--- a/README.md+++ b/README.md@@ -199,7 +199,7 @@ Every page of a combined document renders pixel-identical to its source.```shpdfcraft-cli combine report.pdf appendix.pdf --out combined.pdf-pdfcraft-cli extract report.pdf --pages 1,3,5 --out highlights.pdf+pdfcraft-cli extract report.pdf --pages 1,3,5-8 --out highlights.pdfpdfcraft-cli split report.pdf --every 10 --out-dir parts/```diff --git a/apps/pdfcraft-cli/src/main.rs b/apps/pdfcraft-cli/src/main.rsindex 7f053e055..dd7ca66a0 100644--- a/apps/pdfcraft-cli/src/main.rs+++ b/apps/pdfcraft-cli/src/main.rs@@ -55,7 +55,7 @@ pdfcraft-cli edit <in.pdf> --out out.pdf [--rotate 1,3:90] [--delete 2,4] [--m[--insert-blank 1] [--title T] [--author A] [--full](options run in order; page numbers refer to the document after the earlier options)pdfcraft-cli combine <a.pdf> <b.pdf> … --out combined.pdf-pdfcraft-cli extract <in.pdf> --pages 1,3,5 --out out.pdf+pdfcraft-cli extract <in.pdf> --pages 1,3,5-8 --out out.pdf (pages and ranges, in the order given)pdfcraft-cli split <in.pdf> (--every N | --before 3,7) [--out-dir DIR]pdfcraft-cli check <files or dirs…> [--timeout 20] [--dpi 36] [--json out.json]pdfcraft-cli audit <file.pdf> [--password PW] find faux redactions (fails when found)@@ -381,9 +381,30 @@ fn text(args: &[String]) -> Result<(), CliError> {}}-/// 1-based page list ("1,3,5") → 0-based indices.+/// The most pages one `A-B` range may name: a bound on what a typed range can allocate.+const MAX_RANGE: usize = 1_000_000;++/// 1-based pages ("1,3,5-8": single pages and inclusive ranges, in the order given) → 0-based+/// indices (#1923).fn page_list(s: &str) -> Result<Vec<usize>, String> {- s.split(',').map(|p| p.trim().parse::<usize>().ok().filter(|n| *n > 0).map(|n| n - 1).ok_or(format!("bad page number {p:?}"))).collect()+ let number = |t: &str, item: &str| t.trim().parse::<usize>().ok().filter(|n| *n > 0).ok_or_else(|| format!("bad page number {item:?}"));+ let mut out = Vec::new();+ for item in s.split(',').map(str::trim) {+ match item.split_once('-') {+ Some((from, to)) => {+ let (from, to) = (number(from, item)?, number(to, item)?);+ if to < from {+ return Err(format!("bad page range {item:?}: it ends before it starts"));+ }+ if to - from >= MAX_RANGE {+ return Err(format!("bad page range {item:?}: more than {MAX_RANGE} pages"));+ }+ out.extend(from - 1..to);+ }+ None => out.push(number(item, item)? - 1),+ }+ }+ Ok(out)}/// The options `edit` understands, for its "unknown option" message.diff --git a/apps/pdfcraft-cli/tests/extract.rs b/apps/pdfcraft-cli/tests/extract.rsnew file mode 100644index 000000000..25f1aabc2--- /dev/null+++ b/apps/pdfcraft-cli/tests/extract.rs@@ -0,0 +1,129 @@+//! `extract --pages`: single pages and ranges (#1923), and an out-of-range page named as the+//! user typed it (1-based; it was off by one in 0.7.1).++use std::path::PathBuf;+use std::process::{Command, Output};++const BIN: &str = env!("CARGO_BIN_EXE_pdfcraft-cli");++/// A PDF with `n` empty 200×300 pt pages.+fn fixture(n: usize) -> Vec<u8> {+ let mut objs: Vec<Vec<u8>> = vec![b"<< /Type /Catalog /Pages 2 0 R >>".to_vec()];+ let kids: Vec<String> = (0..n).map(|i| format!("{} 0 R", 3 + i)).collect();+ objs.push(format!("<< /Type /Pages /Kids [{}] /Count {n} /MediaBox [0 0 200 300] >>", kids.join(" ")).into_bytes());+ for _ in 0..n {+ objs.push(b"<< /Type /Page /Parent 2 0 R >>".to_vec());+ }+ let mut out = b"%PDF-1.7+"+ .to_vec();+ let mut offsets = Vec::new();+ for (i, o) in objs.iter().enumerate() {+ offsets.push(out.len());+ out.extend_from_slice(+ format!(+ "{} 0 obj+",+ i + 1+ )+ .as_bytes(),+ );+ out.extend_from_slice(o);+ out.extend_from_slice(+ b"+endobj+",+ );+ }+ let xref = out.len();+ out.extend_from_slice(+ format!(+ "xref+0 {}+0000000000 65535 f+",+ objs.len() + 1+ )+ .as_bytes(),+ );+ for o in offsets {+ out.extend_from_slice(+ format!(+ "{o:010} 00000 n+"+ )+ .as_bytes(),+ );+ }+ out.extend_from_slice(+ format!(+ "trailer+<< /Size {} /Root 1 0 R >>+startxref+{xref}+%%EOF+",+ objs.len() + 1+ )+ .as_bytes(),+ );+ out+}++/// A fresh directory holding `five.pdf` (5 pages).+fn workdir(test: &str) -> PathBuf {+ let d = std::env::temp_dir().join(format!("pdfcraft-cli-extract-{test}-{}", std::process::id()));+ let _ = std::fs::remove_dir_all(&d);+ std::fs::create_dir_all(&d).unwrap();+ std::fs::write(d.join("five.pdf"), fixture(5)).unwrap();+ d+}++fn extract(dir: &std::path::Path, pages: &str) -> Output {+ let out = dir.join(format!("out-{}.pdf", pages.replace(['-', ','], "_")));+ Command::new(BIN).args(["extract", dir.join("five.pdf").to_str().unwrap(), "--pages", pages, "--out", out.to_str().unwrap()]).output().unwrap()+}++/// The page count `info` reports for the extracted file.+fn pages_of(dir: &std::path::Path, pages: &str) -> usize {+ let out = dir.join(format!("out-{}.pdf", pages.replace(['-', ','], "_")));+ let info = Command::new(BIN).args(["info", out.to_str().unwrap()]).output().unwrap();+ let text = String::from_utf8_lossy(&info.stdout);+ let n = text.split("\"pages\":").nth(1).and_then(|s| s.trim().split(|c: char| !c.is_ascii_digit()).next()).unwrap_or("");+ n.parse().unwrap_or_else(|_| panic!("no page count in {text}"))+}++#[test]+fn pages_and_ranges_are_accepted_in_the_order_given() {+ let dir = workdir("ranges");+ for (pages, count) in [("1-3", 3), ("5,1-2", 3), ("2-2", 1), ("1,3,5", 3), (" 4 - 5 , 1 ", 3)] {+ let out = extract(&dir, pages);+ assert!(out.status.success(), "{pages:?}: {}", String::from_utf8_lossy(&out.stderr));+ assert_eq!(pages_of(&dir, pages), count, "{pages:?}");+ }+}++#[test]+fn bad_pages_and_ranges_are_named_as_typed() {+ let dir = workdir("bad");+ for (pages, what) in [+ ("3-1", "bad page range \"3-1\""),+ ("0", "bad page number \"0\""),+ ("1-x", "bad page number \"1-x\""),+ ("2-", "bad page number \"2-\""),+ ("1-9999999", "bad page range \"1-9999999\""),+ ] {+ let out = extract(&dir, pages);+ assert!(!out.status.success(), "{pages:?} succeeded");+ let stderr = String::from_utf8_lossy(&out.stderr);+ assert!(stderr.contains(what), "{pages:?}: {stderr}");+ }+ // A page past the end is named as the user counts it (1-based), as a single page and as the+ // first page a range reaches past the end.+ for (pages, missing) in [("6", "page 6 does not exist"), ("9", "page 9 does not exist"), ("4-7", "page 6 does not exist")] {+ let out = extract(&dir, pages);+ assert!(!out.status.success(), "{pages:?} succeeded");+ let stderr = String::from_utf8_lossy(&out.stderr);+ assert!(stderr.contains(missing), "{pages:?}: {stderr}");+ }+}