Created
November 12, 2025 17:59
-
-
Save su8/9b8ec49d0fe4b0a8957123669f2c218a to your computer and use it in GitHub Desktop.
main15.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use std::process::Command; | |
| use std::path::Path; | |
| use std::path::PathBuf; | |
| use std::fs::read_dir; | |
| use std::env; | |
| fn optimize_pdf(pdf_path: &Path) -> std::io::Result<()> { | |
| let output_path = pdf_path.with_file_name(format!("optimized_{}", pdf_path.file_name().unwrap().to_str().unwrap())); | |
| let mut cmd = Command::new("gs"); | |
| cmd.arg("-sDEVICE=pdfwrite"); | |
| cmd.arg("-dNOPAUSE"); | |
| cmd.arg("-dBATCH"); | |
| cmd.arg("-dQUIET"); | |
| cmd.arg(&format!("-sOutputFile={}", output_path.display())); | |
| cmd.arg(pdf_path.display()); | |
| cmd.spawn()?.wait()?; | |
| Ok(()) | |
| } | |
| fn main() { | |
| let mut pdf_files = Vec::new(); | |
| for entry in read_dir(".").unwrap() { | |
| let entry = entry.unwrap(); | |
| let path = entry.path(); | |
| if path.is_file() && path.extension().unwrap() == "pdf" { | |
| pdf_files.push(path); | |
| } | |
| } | |
| let num_cpus = num_cpus::get(); | |
| let pool = rayon::ThreadPoolBuilder::new().num_threads(num_cpus as usize).build().unwrap(); | |
| pool.install(); | |
| pool.scope(|s| { | |
| s.spawn(move || { | |
| for pdf_file in pdf_files { | |
| optimize_pdf(&pdf_file).unwrap(); | |
| } | |
| }); | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment