Remap all color space operations in a PDF to DeviceGray to produce a greyscale-only file.
use pdfluent::{Document, color::GreyscaleOptions};
fn main() -> pdfluent::Result<()> {
let mut doc = Document::open("input.pdf")?;
doc.convert_to_greyscale(GreyscaleOptions::default())?;
doc.save("greyscale.pdf")?;
Ok(())
}Load the color PDF into a mutable Document.
let mut doc = Document::open("color_input.pdf")?;GreyscaleOptions controls the luminance formula and whether images are converted in place.
use pdfluent::color::GreyscaleOptions;
let opts = GreyscaleOptions::default()
.convert_images(true) // downsample color images to greyscale
.luminance_formula(pdfluent::color::LuminanceFormula::Bt709); // standard TV luminanceconvert_to_greyscale rewrites all color operators (rg, RG, k, K, cs, CS) and converts embedded images to /DeviceGray.
doc.convert_to_greyscale(opts)?;Inspect the color spaces in the output to confirm conversion.
for page in doc.pages() {
for cs in page.color_spaces() {
println!("page color space: {:?}", cs);
}
}Write the converted document. File size typically decreases because image data is smaller without color channels.
doc.save("greyscale.pdf")?;No JVM, no runtime, no DLL dependencies. Ships as a single native binary or WASM module.
Rust's ownership model prevents buffer overflows and use-after-free. No segfaults in PDF parsing.
Same code runs server-side, in Docker, on AWS Lambda, on Cloudflare Workers, or in the browser via WASM.