Supply the user password or owner password to decrypt and open a password-protected PDF document.
use pdfluent::{Document, OpenOptions};
fn main() -> pdfluent::Result<()> {
let doc = Document::open_with_options(
"protected.pdf",
OpenOptions::default().password("s3cr3t"),
)?;
println!("Pages: {}", doc.page_count());
println!("Text: {}", doc.page(0)?.extract_text()?);
Ok(())
}Attempt a standard open and check for the WrongPassword or EncryptedDocument error before prompting for a password.
use pdfluent::{Document, Error};
match Document::open("file.pdf") {
Ok(doc) => { /* not encrypted */ }
Err(Error::EncryptedDocument) => {
println!("Document requires a password.");
}
Err(e) => return Err(e),
}The user password grants read access. Pass it via OpenOptions::password.
use pdfluent::{Document, OpenOptions};
let doc = Document::open_with_options(
"protected.pdf",
OpenOptions::default().password("user_password"),
)?;The owner password unlocks all permissions including printing, copying, and modification. Use OpenOptions::owner_password.
let doc = Document::open_with_options(
"protected.pdf",
OpenOptions::default().owner_password("owner_password"),
)?;After opening with a user password, check what operations are permitted by the document encryption dictionary.
let perms = doc.permissions();
println!("Print: {}", perms.can_print());
println!("Copy: {}", perms.can_copy());
println!("Modify: {}", perms.can_modify());
println!("Fill forms: {}", perms.can_fill_forms());
println!("Extract text: {}", perms.can_extract_text());If you have the owner password, remove encryption to produce an unprotected file.
let mut doc = Document::open_with_options(
"protected.pdf",
OpenOptions::default().owner_password("owner_pw"),
)?;
doc.remove_encryption()?;
doc.save("unprotected.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.