【问题标题】:Decrypt an encrypted pdf using MPDF使用 MPDF 解密加密的 pdf
【发布时间】:2020-06-16 08:39:42
【问题描述】:

有没有办法使用 MPDF 库 (PHP) 解密 pdf?

我收到了来自第 3 方的 pdf,他们正在使用 FPDI 对其进行加密。我正在使用MPDF 使其受密码保护。在访问加密的 pdf 时,如果没有加密,很容易制作受密码保护的 pdf 我收到此异常:

   This PDF document is encrypted and cannot be processed with FPDI

我相信这是不可能的,一旦我有解密 pdf 的密钥就可以做到这一点,仍然想再次确认是否有办法绕过这个?

适用于非加密 pdf。

    try{
        $mpdf = new \Mpdf\Mpdf();
        $filename = "pdfs/signed.pdf";
        password="Test";
        $pagecount = $mpdf->SetSourceFile($filename); //use any pdf you want
        for ($i= 1; $i<=$pagecount; $i++ ){
            $tplId = $mpdf->importPage($i);
            $mpdf->UseTemplate($tplId);
            $mpdf->AddPage();
        }

        echo "setting protection </br>";
        $mpdf->SetProtection(array(), $password, $password);
        echo "saving file: </br>";
        $mpdf->Output($filename, \Mpdf\Output\Destination::FILE);

        echo "done";

     } catch (Exception $ex) {
         echo "exception : ";
         echo $ex->getMessage();
     } 

【问题讨论】:

    标签: pdf encryption mpdf fpdi


    【解决方案1】:

    FPDI cannot import pages of encrypted PDF documents.

    如果您知道所有者密码,您可以在将其传递给 FPDI 之前使用SetaPDF-Core 组件(不是免费的)对其进行解密:

    $writer = new SetaPDF_Core_Writer_File('result.pdf');
    $document = SetaPDF_Core_Document::loadByFilename('encrypted.pdf', $writer);
    
    // get an instance of the security handler
    $secHandler = $document->getSecHandler();
    if ($secHandler) {
        // try to authenticate as the owner
        if (!$secHandler->authByOwnerPassword('OWNER PASSWORD')) {
            throw new Exception('Unable to authenticate as "owner".');
        }
    
        // remove security handler
        $document->setSecHandler(null);
    }
    
    $document->save()->finish();
    

    【讨论】:

      猜你喜欢
      • 2016-11-07
      • 1970-01-01
      • 1970-01-01
      • 2020-10-18
      • 2013-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多