How-to guides/Security & Encryption

Remove the password from a PDF you own in Rust

Decrypt a password-protected PDF and save a clean, unencrypted copy. Requires the owner password.

rust
use pdfluent::PdfDocument;

fn main() -> pdfluent::Result<()> {
    // Open with the owner password (or user password)
    let mut doc = PdfDocument::open_with_password(
        "protected.pdf",
        "owner-pw-123",
    )?;

    doc.remove_encryption()?;
    doc.save("unprotected.pdf")?;
    println!("Password removed. Saved unprotected.pdf.");

    Ok(())
}
Install:cargo add pdfluentDownload SDK →

Step by step

1

Add PDFluent to Cargo.toml

Encryption removal is part of the encryption feature.

rust
# Cargo.toml
[dependencies]
pdfluent = { version = "0.9", features = ["encryption"] }
2

Open the PDF with the owner password

You need the owner password to remove encryption. The user password only grants read access. If you only have the user password, you cannot remove or modify the encryption.

rust
use pdfluent::PdfDocument;

let mut doc = PdfDocument::open_with_password(
    "protected.pdf",
    "owner-pw-123",
)?;
3

Handle the wrong password case

open_with_password() returns Error::WrongPassword if the password is incorrect. Handle this in a pipeline to log failures without crashing.

rust
use pdfluent::{PdfDocument, Error};

match PdfDocument::open_with_password("protected.pdf", "owner-pw-123") {
    Ok(doc) => {
        println!("Opened successfully");
        // ... proceed
    }
    Err(Error::WrongPassword) => {
        eprintln!("Wrong password. Cannot remove encryption.");
    }
    Err(Error::PasswordRequired) => {
        eprintln!("File is encrypted but no password was supplied.");
    }
    Err(e) => eprintln!("Error: {}", e),
}
4

Remove encryption and save

remove_encryption() strips all encryption dictionaries from the document. The saved file is a plain PDF with no password required.

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

// Verify by opening without a password
let check = PdfDocument::open("unprotected.pdf")?;
println!("Encrypted: {}", check.is_encrypted()); // false
5

Batch remove passwords from multiple PDFs

Combine with a directory scan to decrypt a folder of PDFs at once.

rust
use std::fs;
use pdfluent::{PdfDocument, Error};

let password = "owner-pw-123";

for entry in fs::read_dir("./encrypted")?.filter_map(|e| e.ok()) {
    let path = entry.path();
    if path.extension().map_or(false, |e| e == "pdf") {
        match PdfDocument::open_with_password(&path, password) {
            Ok(mut doc) => {
                doc.remove_encryption()?;
                let out = std::path::Path::new("./decrypted")
                    .join(path.file_name().unwrap());
                doc.save(out)?;
                println!("Decrypted: {}", path.display());
            }
            Err(Error::WrongPassword) => {
                eprintln!("Wrong password: {}", path.display());
            }
            Err(e) => eprintln!("Error {}: {}", path.display(), e),
        }
    }
}

Notes and tips

  • You must have the owner password to remove encryption. PDFluent does not brute-force or crack passwords.
  • If a PDF only has an owner password (empty user password), the file opens without a password but is still encrypted. You still need the owner password to call remove_encryption().
  • Saving to the same file path as the input overwrites the original. Keep a backup of the original before running this operation.

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