有什么方法可以使用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();
}
如果你知道所有者的密码,你可以通过使用 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();