Open an encrypted PDF with the user or owner password and save an unprotected copy. Works with RC4-128 and AES-128/256 encrypted files.
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("decrypted.pdf")?;
Ok(())
}Use PdfDocument::open_with() and provide the user or owner password. The method returns an error if the password is wrong.
use pdfluent::{PdfDocument, OpenOptions};
let mut doc = PdfDocument::open_with("protected.pdf", OpenOptions::new().with_password("open123"))?;Match on Error::WrongPassword to give the user a clear error instead of a generic failure.
use pdfluent::{PdfDocument, OpenOptions};
let result = PdfDocument::open_with("protected.pdf", OpenOptions::new().with_password("guess"));
match result {
Ok(doc) => { /* proceed */ }
Err(_) => eprintln!("Could not open - wrong or missing password."),
}After opening, inspect the encryption type and permission flags that were set by the document author.
// Strip the encryption so the saved copy opens without a password
doc.decrypt("open123")?;Call remove_encryption() to strip all password protection and permission flags from the document in memory.
doc.save("decrypted.pdf")?;Save to a new path. The saved file will open in any PDF reader without a password.
// Verify by re-opening without a password
let check = PdfDocument::open("decrypted.pdf")?;
println!("Re-opened without a password: {} pages", check.page_count());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.