Skip to content

Instantly share code, notes, and snippets.

@su8
Created November 12, 2025 17:59
Show Gist options
  • Select an option

  • Save su8/9b8ec49d0fe4b0a8957123669f2c218a to your computer and use it in GitHub Desktop.

Select an option

Save su8/9b8ec49d0fe4b0a8957123669f2c218a to your computer and use it in GitHub Desktop.
main15.rs
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