Reputation: 299
We are using FPDI to attach an extra Page with status information to incoming PDF Documents. We do this, by loading the existing PDF as Template and then we add a new Page in which the neccessary Status Information can be found. Unfortunately there is no other way transporting those status information further, than doing so by adding an extra Page to the PDF.
We stumbled across some encrypted PDF's now. You can open those PDF's in any PDF Viewer and Browser properly. But FPDI does not support the loading of encrypted PDF's, our Code stops with the Error:
This PDF document is encrypted and cannot be processed with FPDI
I want to decrypt those PDF's, that are viewable without entering a password, before they are processed. In my mind there would be two ways:
What dou you think? Are there any better Ideas out there? I would appreciate it! Thanks.
Upvotes: -1
Views: 5887
Reputation: 85
If you're using Fpdi, you can check file permissions before adding a file. This function can determine if a file is protected.
function isPdfProtected($pdfPath) {
try {
// Try to read the file usng FPDI
$pdf = new \setasign\Fpdi\Fpdi();
$pageCount = $pdf->setSourceFile($pdfPath);
return false; // file not protected
} catch (\Exception $e) {
// the file is protected
if (strpos($e->getMessage(), 'encrypted') !== false) {
return true;
}
// another exceptions
return false;
}
}
You can then check the file permissions before adding the file.
Upvotes: -2
Reputation: 5058
With the SetaPDF-Core it is possible to authenticate to an encrypted/protected PDF document:
$document = SetaPDF_Core_Document::loadByFilename('encrypted.pdf');
To check if a document is encrypted you can simply check for a security handler:
$isEncrypted = $document->hasSecHandler();
Depending on this information you can access the security handler:
if ($isEncrypted) {
// get the security handler
$secHandler = $document->getSecHandler();
// authenticate with a password without knowing if it is the owner or user password:
if ($secHandler->auth('a secret password')) {
echo 'authenticated as ' . $secHandler->getAuthMode();
} else {
echo 'authentication failed - neither user nor owner password did match.';
}
// authenticate with the user password:
if ($secHandler->authByUserPassword('a secret password')) {
echo 'authenticated as user';
} else {
echo 'authentication failed with the user password.';
}
// authenticate with the owner password:
if ($secHandler->authByOwnerPassword('a secret password')) {
echo 'authenticated as owner';
} else {
echo 'authentication failed with the owner password.';
}
}
(This is also possible with a private key and a certficiate if the document is encrypted with its public key - for more information see here)
If you are authenticated as the owner, you can remove the securtiy handler from the document:
if ($secHandler->getAuthMode() === SetaPDF_Core_SecHandler::OWNER) {
$document->setSecHandler(null);
$writer = new SetaPDF_Core_Writer_File('not-encrypted.pdf');
$document->setWriter($writer);
$document->save()->finish();
}
Upvotes: 2