How to fix the PDF encrypted error in PDFluent

This error means PDFluent found an encryption dictionary in the PDF and cannot proceed without credentials. The fix depends on whether you have the user password, owner password, or the file has an empty password.
Error::Encrypted
Error: document is encrypted and requires a password

Why this happens

The PDF has a user password set

The author protected the document with a password. Any reader must supply the correct user password before opening. PDFluent detects the encryption dictionary and returns this error instead of attempting to parse an unreadable stream.

The password string is an empty string, not None

Some PDFs are encrypted with an empty string as the password (""). Opening the file without passing any password triggers the error, but passing an explicit empty string succeeds. This is common with PDFs exported from older tools.

You have the user password but the operation requires the owner password

PDF encryption has two password levels: user and owner. The owner password unlocks permissions like printing, copying, and editing. If you open with the user password and then attempt a restricted operation (like flattening or signing), PDFluent returns a permissions error that looks similar to this one.

How to fix it

  1. Open with the user password

    Pass the password to Document::open_protected(). The password is a byte slice, not a string, because PDF passwords are byte sequences.

    code
    use pdfluent::PdfDocument;
    
    let doc = Document::open_protected("locked.pdf", b"mypassword")?;
    let text = doc.page(1)?.text()?;
  2. Try an empty password first

    If you receive this error on a PDF you did not expect to be protected, try opening with an empty password. Many PDF generators set empty-string encryption.

    code
    use pdfluent::PdfDocument;
    
    // Try empty password before concluding the document is inaccessible
    let doc = Document::open_protected("export.pdf", b"")?;
  3. Open with the owner password for restricted operations

    If you need to modify the document (flatten, sign, redact), you need the owner password. Use Document::open_owner() to unlock full permissions.

    code
    use pdfluent::{OpenOptions, PdfDocument};
    
    let mut doc = PdfDocument::open_with(
        "protected.pdf",
        OpenOptions::new().with_password("ownerpassword"),
    )?;
    
    // Now permitted to flatten, sign, or change permissions
    doc.flatten_forms()?;
    doc.save("output.pdf")?;

Frequently asked questions

Can PDFluent crack or bypass PDF encryption?
No. PDFluent enforces PDF encryption standards. If you do not have the password, PDFluent cannot open the document.
The error says "encrypted" but I know the file is not password-protected.
Some PDFs set an encryption dictionary with an empty owner password to enforce permissions flags (print-only, no-copy, etc.) without a visible password prompt. Try opening with Document::open_protected("file.pdf", b"") to see if an empty password resolves it.
How do I check if a PDF is encrypted before trying to open it?
Use Document::is_encrypted() which reads only the PDF header and encryption dictionary without decrypting the full file.