Error: document is encrypted and requires a passwordThis 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.
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.
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.
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.
Pass the password to Document::open_protected(). The password is a byte slice, not a string, because PDF passwords are byte sequences.
use pdfluent::Document;
let doc = Document::open_protected("locked.pdf", b"mypassword")?;
let text = doc.page(0)?.extract_text()?;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.
use pdfluent::Document;
// Try empty password before concluding the document is inaccessible
let doc = Document::open_protected("export.pdf", b"")?;If you need to modify the document (flatten, sign, redact), you need the owner password. Use Document::open_owner() to unlock full permissions.
use pdfluent::Document;
let mut doc = Document::open_owner("protected.pdf", b"ownerpassword")?;
// Now permitted to flatten, sign, or change permissions
doc.acroform()?.flatten()?;
doc.save("output.pdf")?;