Lock down what readers can do with your PDF. Restrict printing, copying text, editing, and form filling using the PDFluent SDK.
use pdfluent::{PdfDocument, EncryptionOptions, Permissions};
fn main() -> pdfluent::Result<()> {
let mut doc = PdfDocument::open("input.pdf")?;
let permissions = Permissions::builder()
.allow_printing(false)
.allow_copying(false)
.allow_editing(false)
.allow_form_filling(true)
.build();
let opts = EncryptionOptions::aes256("owner-secret", "user-secret")
.with_permissions(permissions);
doc.encrypt(opts)?;
doc.save("locked.pdf")?;
Ok(())
}Add the dependency to Cargo.toml. The encryption feature includes AES-128 and AES-256 support.
# Cargo.toml
[dependencies]
pdfluent = { version = "0.9", features = ["encryption"] }Open the PDF you want to restrict. If it is already encrypted you must supply the owner password.
let mut doc = PdfDocument::open("input.pdf")?;
// Or, if already encrypted:
// let mut doc = PdfDocument::open_with_password("input.pdf", "current-owner-pw")?;Use the Permissions builder to choose what is allowed. All operations default to denied when you use the builder.
use pdfluent::Permissions;
let permissions = Permissions::builder()
.allow_printing(false) // block all printing
.allow_high_res_printing(false)
.allow_copying(false) // block text/image copying
.allow_editing(false) // block page editing
.allow_annotations(false) // block adding comments
.allow_form_filling(true) // allow form data entry
.build();Pair the permission set with an encryption config. The owner password lets you change permissions later. The user password is what readers enter to open the file.
use pdfluent::EncryptionOptions;
let opts = EncryptionOptions::aes256("owner-secret", "user-secret")
.with_permissions(permissions);
doc.encrypt(opts)?;Write the encrypted PDF to disk. The original file is not modified.
doc.save("locked.pdf")?;
println!("Saved locked.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.