【问题标题】:Decrypt encrypted PDF to load in FPDI解密加密的 PDF 以加载到 FPDI
【发布时间】:2020-01-08 10:49:20
【问题描述】:

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

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

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

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

  1. 使用 PHP 类或其他任何东西解密 PDF
  2. 在我的脚本中使用打印机驱动程序将 PDF 打印到 PDF

你觉得呢?有没有更好的想法? 我会很感激! 谢谢。

【问题讨论】:

  • 我们(FPDI 的作者)还提供额外的非免费工具,允许您在加密文档中进行身份验证。因此,如果非免费解决方案对您有用,并且您知道这些文档的所有者密码,我很高兴给出答案。

标签: php pdf encryption fpdi


【解决方案1】:

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

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-07
    • 2013-09-06
    • 1970-01-01
    • 2013-12-28
    • 2018-01-13
    • 2013-03-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多