Protect a PDF with a user password and an owner password. Set 128-bit RC4 or 256-bit AES encryption and fine-grained permission flags.
use pdfluent::{PdfDocument, EncryptOptions, PdfPermissions};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut doc = PdfDocument::open("report.pdf")?;
let opts = EncryptOptions::aes256()
.user_password("open123")
.owner_password("owner_secret")
.permissions(PdfPermissions::all_except_edit());
doc.encrypt(&opts)?;
doc.save("report_protected.pdf")?;
Ok(())
}Open the document you want to protect. Use a mutable binding.
use pdfluent::PdfDocument;
let mut doc = PdfDocument::open("report.pdf")?;EncryptOptions::aes256() uses AES-256, which is the current best practice. Use EncryptOptions::aes128() if you need compatibility with older readers.
use pdfluent::EncryptOptions;
let opts = EncryptOptions::aes256();The user password is required to open the document. The owner password unlocks all permissions. If you omit user_password, the document opens without a password but still enforces permission flags.
let opts = opts
.user_password("open_document_2025")
.owner_password("full_access_secret");PdfPermissions lets you allow or deny printing, copying text, modifying content, and filling forms.
use pdfluent::PdfPermissions;
let perms = PdfPermissions::default()
.allow_printing(true)
.allow_copy_text(false)
.allow_modify(false)
.allow_fill_forms(true);
let opts = opts.permissions(perms);Call encrypt() then save() to write the protected file.
doc.encrypt(&opts)?;
doc.save("report_protected.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.