Security, Permissions & Options
php-pdftk provides standard PDF security features, allowing you to encrypt documents, require passwords for viewing, and restrict what users can do with the file once opened.
Passwords & Encryption
PDFs utilize two types of passwords:
- User Password: Required to open and view the document.
- Owner Password: Required to change permissions or bypass restrictions (like printing).
If you set an Owner password but not a User password, anyone can open the document, but they will be subject to the permissions you define.
use mikehaertl\pdftk\Pdf;
$pdf = new Pdf('/path/to/contract.pdf');
$pdf->setPassword('admin_secret') // Sets the Owner password
->setUserPassword('client_secret') // Sets the User password
->passwordEncryption(128) // Use 128-bit encryption (default)
->saveAs('/path/to/secured_contract.pdf');
Removing Passwords
To unencrypt a document, instantiate a Pdf object, provide the password to the input file, and save it without setting any new security options.
$pdf = new Pdf();
// Pass the password as the third argument to addFile
$pdf->addFile('/path/to/protected.pdf', 'A', 'admin_secret')
->saveAs('/path/to/unprotected.pdf');
Setting Permissions
You can explicitly restrict actions in the PDF using the allow() method. You must pass a space-separated string of allowed privileges.
Note: To enforce permissions, you must set an Owner password.
$pdf = new Pdf('/path/to/ebook.pdf');
// Allow high-res printing and accessibility screen readers, but nothing else.
$pdf->allow('Printing ScreenReaders')
->setPassword('owner_secret')
->saveAs('/path/to/restricted_ebook.pdf');
Available Permission Flags:
Printing(High quality printing)DegradedPrinting(Low quality printing)ModifyContents(Editing the document)Assembly(Inserting/rotating pages)CopyContents(Copying text/graphics to clipboard)ScreenReaders(Accessibility extraction)ModifyAnnotations(Commenting)FillIn(Filling interactive form fields)AllFeatures(Allows everything)
Useful Modifiers & Options
These methods toggle various processing flags in pdftk and can be chained onto your main operations.
flatten()
Merges interactive elements (like form fields) into the static visual layer of the document. The fields will no longer be editable.
Warning: Flattening in pdftk strips the needAppearances flag, which often destroys UTF-8 characters rendered in forms. Avoid flatten() if your forms contain non-ASCII characters.
compress($compress = true)
Re-compresses page streams to reduce file size. Passing false uncompresses the streams (useful if you need to open the raw PDF in a text editor to debug its structure).
dropXfa() & dropXmp()
Modern PDFs sometimes contain legacy formats (AcroForm, Info Directory) alongside newer XML-based formats (XFA, XMP). Because pdftk only writes to the older formats, PDF readers might ignore your changes in favor of the unchanged newer XML data.
dropXfa(): Drops XFA data. Automatically called byfillForm()to ensure AcroForm data is visible.dropXmp(): Drops XMP metadata. You should chain this when usingupdateInfo().
$pdf = new Pdf('/path/to/document.pdf');
$pdf->updateInfo($data)
->dropXmp()
->compress()
->saveAs('/path/to/final.pdf');