Decrypt a password-protected PDF and save a clean, unencrypted copy. Requires the owner password.
use pdfluent::{PdfDocument, OpenOptions};
fn main() -> pdfluent::Result<()> {
let mut doc = PdfDocument::open_with(
"locked.pdf",
OpenOptions::new().with_password("s3cret"),
)?;
doc.decrypt("s3cret")?;
doc.save("unlocked.pdf")?;
Ok(())
}Encryption removal is part of the encryption feature.
# Cargo.toml
[dependencies]
pdfluent = "1.0.0-beta.8"You need the owner password to remove encryption. The user password only grants read access. If you only have the user password, you cannot remove or modify the encryption.
use pdfluent::{PdfDocument, OpenOptions};
let mut doc = PdfDocument::open_with(
"protected.pdf",
OpenOptions::new().with_password("owner-pw-123"),
)?;open_with_password() returns Error::WrongPassword if the password is incorrect. Handle this in a pipeline to log failures without crashing.
use pdfluent::{PdfDocument, OpenOptions};
match PdfDocument::open_with("protected.pdf", OpenOptions::new().with_password("owner-pw-123")) {
Ok(doc) => println!("Opened successfully"),
Err(_) => eprintln!("Wrong or missing password."),
}remove_encryption() strips all encryption dictionaries from the document. The saved file is a plain PDF with no password required.
doc.decrypt("owner-pw-123")?;
doc.save("unprotected.pdf")?;
// Verify by opening without a password
let _check = PdfDocument::open("unprotected.pdf")?;
println!("Saved an unprotected copy.");Combine with a directory scan to decrypt a folder of PDFs at once.
use std::fs;
use pdfluent::{PdfDocument, OpenOptions};
let password = "owner-pw-123";
for entry in fs::read_dir("./encrypted")?.filter_map(|e| e.ok()) {
let path = entry.path();
if path.extension().map_or(false, |e| e == "pdf") {
match PdfDocument::open_with(&path, OpenOptions::new().with_password(password)) {
Ok(mut doc) => {
doc.decrypt(password)?;
let out = std::path::Path::new("./decrypted").join(path.file_name().unwrap());
doc.save(out)?;
println!("Decrypted: {}", path.display());
}
Err(_) => eprintln!("Could not open: {}", path.display()),
}
}
}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.