How-to guides/Security & Encryption

Open and read a password-protected PDF in Rust

Supply the user password or owner password to decrypt and open a password-protected PDF document.

rust
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(())
}
Install:cargo add pdfluentDownload SDK →

Step by step

1

Detect if a PDF is encrypted

Attempt a standard open and check for the WrongPassword or EncryptedDocument error before prompting for a password.

rust
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),
}
2

Open with the user password

The user password grants read access. Pass it via OpenOptions::password.

rust
use pdfluent::{Document, OpenOptions};

let doc = Document::open_with_options(
    "protected.pdf",
    OpenOptions::default().password("user_password"),
)?;
3

Open with the owner password

The owner password unlocks all permissions including printing, copying, and modification. Use OpenOptions::owner_password.

rust
let doc = Document::open_with_options(
    "protected.pdf",
    OpenOptions::default().owner_password("owner_password"),
)?;
4

Check document permissions

After opening with a user password, check what operations are permitted by the document encryption dictionary.

rust
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());
5

Remove encryption and save an unprotected copy

If you have the owner password, remove encryption to produce an unprotected file.

rust
let mut doc = Document::open_with_options(
    "protected.pdf",
    OpenOptions::default().owner_password("owner_pw"),
)?;

doc.remove_encryption()?;
doc.save("unprotected.pdf")?;

Notes and tips

  • PDF encryption uses AES-256 (PDF 2.0), AES-128 (PDF 1.6+), or RC4-128/40. PDFluent supports all three for opening.
  • The user password is used for routine reading. The owner password was intended to enforce restrictions but PDF encryption does not prevent all bypasses.
  • An empty string is a valid user password. Some protected PDFs have no user password (only restrictions) and can be opened without supplying a password.
  • Text extraction is subject to the can_extract_text permission. PDFluent enforces this check unless the owner password was used.

Why PDFluent for this

Pure Rust

No JVM, no runtime, no DLL dependencies. Ships as a single native binary or WASM module.

Memory safe

Rust's ownership model prevents buffer overflows and use-after-free. No segfaults in PDF parsing.

Runs anywhere

Same code runs server-side, in Docker, on AWS Lambda, on Cloudflare Workers, or in the browser via WASM.

Frequently asked questions