Skip to Content
NexPDF

Encryption and permissions

Open password-protected PDFs, encrypt with AES-256 and permissions, remove passwords, and stamp watermarks, headers and Bates numbers with NexPDF for PHP.

English

License module

Encrypting, removing passwords, watermarks, headers, footers and Bates numbering need the security module (Startup tier and above). Opening encrypted files and reading their security settings work without a key.

Open an encrypted PDF

use Nexdevit\NexPdf\Document;
use Nexdevit\NexPdf\NexPdfException;

try {
    $doc = Document::open('protected.pdf', 'secret');
} catch (NexPdfException $e) {
    echo $e->getMessage();   // "password required" or "incorrect password"
}

The user (open) password or the owner password both work. RC4, AES-128 and AES-256 files are supported. Document::openBytes($data, 'secret') does the same for bytes.

$info = json_decode($doc->securityJson(), true);
var_dump($info['encrypted'], $info['owner'], $info['print'], $info['copy']);

securityJson() reports encrypted, revision, owner (opened with owner access) and the permissions print, modify, copy, annotate, fill_forms and assemble.

Encrypt with AES-256

$doc->encryptAes256('user-password', 'owner-password');
$doc->save('encrypted.pdf');

An empty user password lets anyone open the file while the owner password still protects the permissions.

Set permissions

encryptWithPermissions($userPw, $ownerPw, $permissions) takes a bit mask of what users without the owner password may do:

FlagValue
Print1
Modify2
Copy text and images4
Annotate8
Fill forms16
Assemble (insert, rotate, delete pages)32
const PRINT_PAGES = 1;
const FILL_FORMS = 16;
$doc->encryptWithPermissions('', 'owner-password', PRINT_PAGES | FILL_FORMS);
$doc->save('print-and-fill-only.pdf');

Remove a password

$doc = Document::open('encrypted.pdf', 'owner-password');
$doc->removePassword();     // or pass the owner password here
$doc->save('decrypted.pdf');

Watermarks, headers, footers and Bates numbers

$doc->addWatermark('CONFIDENTIAL');
$doc->addHeaderFooter('Acme, Lda', 'Page {page} of {total}', 9);   // font size 0 = default
$doc->addBates('ACME-', 1, 6);                                       // ACME-000001, ACME-000002, ...
$doc->removeWatermarks();                                            // removes NexPDF watermarks

On this page