PDF Encrypt
PdfEncrypt applies or removes PDF encryption. It performs a full document rewrite (not incremental), rebuilding the page tree with the new encryption settings.
Opening a PDF
Section titled “Opening a PDF”use Phpdftk\Pdf\Toolkit\PdfEncrypt;
// Unencrypted PDF$enc = PdfEncrypt::open('doc.pdf');
// Already encrypted -- supply the password to open$enc = PdfEncrypt::open('encrypted.pdf', password: 'secret');
// From string$enc = PdfEncrypt::openString($pdfBytes);Encrypting a PDF
Section titled “Encrypting a PDF”use Phpdftk\Pdf\Toolkit\Encryption\EncryptionMethod;
$enc->encrypt('userpass', 'ownerpass', EncryptionMethod::Aes256) ->save('encrypted.pdf');Encryption methods
Section titled “Encryption methods”| Enum case | Algorithm |
|---|---|
EncryptionMethod::Rc440 | RC4 40-bit |
EncryptionMethod::Rc4128 | RC4 128-bit |
EncryptionMethod::Aes128 | AES 128-bit |
EncryptionMethod::Aes256 | AES 256-bit (default) |
The default when no method is specified is Aes256.
Decrypting a PDF
Section titled “Decrypting a PDF”Remove all encryption, producing an unprotected document:
PdfEncrypt::open('encrypted.pdf', password: 'secret') ->decrypt() ->save('decrypted.pdf');Changing passwords
Section titled “Changing passwords”PdfEncrypt::open('encrypted.pdf', password: 'oldpass') ->changePasswords('newuser', 'newowner') ->save('rekeyed.pdf');If no encryption method has been explicitly set, changePasswords defaults to AES-256.
Setting permissions
Section titled “Setting permissions”Use the Permission constants to control what operations are allowed:
use Phpdftk\Pdf\Toolkit\Encryption\Permission;
$enc->encrypt('user', 'owner', EncryptionMethod::Aes256, Permission::PRINT | Permission::COPY);Available permissions
Section titled “Available permissions”| Constant | Description |
|---|---|
Permission::PRINT | Print the document |
Permission::MODIFY | Modify document contents |
Permission::COPY | Copy or extract text/graphics |
Permission::ANNOTATE | Add or modify annotations |
Permission::FILL_FORMS | Fill in form fields |
Permission::ACCESSIBILITY | Extract text for accessibility |
Permission::ASSEMBLE | Assemble the document (insert, rotate, delete pages) |
Permission::PRINT_HIGH | High-resolution printing |
Permission::ALL | All permissions granted |
Permissions can also be set separately:
$enc->setPermissions(Permission::PRINT | Permission::FILL_FORMS);Querying encryption status
Section titled “Querying encryption status”$enc->isEncrypted(); // boolSaving
Section titled “Saving”// To file$enc->save('output.pdf');
// To string$bytes = $enc->toBytes();Document info
Section titled “Document info”$enc->getPageCount(); // intEscape hatch
Section titled “Escape hatch”$reader = $enc->getReader(); // PdfReader