解密加密的 PDF 以在 FPDI 中加载



我们使用FPDI将带有状态信息的额外页面附加到传入的PDF文档中。为此,我们将现有PDF加载为模板,然后添加一个新页面,在其中可以找到必要的状态信息。不幸的是,除了在 PDF 中添加一个额外的页面之外,没有其他方法可以进一步传输这些状态信息。

我们现在偶然发现了一些加密的PDF。您可以在任何PDF查看器和浏览器中正确打开这些PDF。但是FPDI不支持加载加密的PDF,我们的代码因错误而停止:

此 PDF 文档已加密,无法使用 FPDI 进行处理

我想解密那些无需输入密码即可查看的 PDF,然后再进行处理。在我看来,有两种方法:

  1. 用PHP类或任何东西解密PDF,那是在那里
  2. 使用打印机驱动程序将脚本中的 PDF 打印为 PDF

你觉得呢?有没有更好的想法? 我将不胜感激! 谢谢。

使用SetaPDF-Core,可以对加密/受保护的PDF文档进行身份验证:

$document = SetaPDF_Core_Document::loadByFilename('encrypted.pdf');

要检查文档是否加密,您只需检查安全处理程序:

$isEncrypted = $document->hasSecHandler();

根据此信息,您可以访问安全处理程序:

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.';
}
}

(如果文档使用其公钥加密,则也可以使用私钥和证书 - 有关更多信息,请参阅此处(

如果您被认证为所有者,则可以从文档中删除安全处理程序:

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();
}

最新更新