代码之家  ›  专栏  ›  技术社区  ›  Danyal Sandeelo

使用MPDF解密加密的pdf

  •  0
  • Danyal Sandeelo  · 技术社区  · 4 年前

    有什么方法可以使用MPDF库(PHP)解密pdf吗?

    我收到了来自第三方的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();
         } 
    
    0 回复  |  直到 4 年前
        1
  •  1
  •   Jan Slabon    4 年前

    FPDI cannot import pages of encrypted PDF documents .

    如果您知道所有者密码,则可以使用 SetaPDF-Core 在将组件传递给FPDI之前:

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