PHP v7.0.27 中的密码保护和禁用编辑 PDF
Password Protect & Disable Editing PDF in PHP v7.0.27
我正在使用 CentOS7,我的后端是 PHP v7.0.27 和 Laravel 框架 v4.2。我正在使用 Base64 数据保存 PDF(解码 base64 后)。我想用密码保护和禁用 PDF 编辑。我一直在寻找实现这一目标的方法,但到目前为止还没有成功,我知道这可以通过 Dompdf 实现,但为此需要 HTML 数据来呈现 PDF。如果可能,请告诉我。谢谢!
下面是我用来保存 PDF 的代码
private function decodeBase64ToPdf($fileToReadFrom, $fileToSaveTo)
{
$pdf_base64_handler = fopen($fileToReadFrom,'r'); //Get encoded file content from txt file
$pdf_content = fread($pdf_base64_handler, filesize($fileToReadFrom));
fclose($pdf_base64_handler);
$pdf_decoded = base64_decode($pdf_content); //Decode pdf content
$file = fopen($fileToSaveTo,'w'); //Write decoded data to pdf file
fwrite($file, $pdf_decoded);
if(fclose($file)) //Close output file
{
return true;
}
return false;
}
在测试了很多不同的东西之后。我通过安装 FPDI 和 FPDF 解决了这个问题。
链接:
FPDF FPDI
private function decodeBase64ToPdf($fileToReadFrom, $fileToSaveTo)
{
$pdf_base64_handler = fopen($fileToReadFrom,'r'); //Get encoded file content from txt file
$pdf_content = fread($pdf_base64_handler, filesize($fileToReadFrom));
fclose($pdf_base64_handler);
$pdf_decoded = base64_decode($pdf_content); //Decode pdf content
$file = fopen($fileToSaveTo,'w'); //Write decoded data to pdf file
fwrite($file, $pdf_decoded);
if(fclose($file)) //Close output file
{
$this->passwordProtect($fileToSaveTo);
return true;
}
return false;
}
private function passwordProtect($pdfPath)
{
$passPdf = new FpdiProtection();
//calculate the number of pages from the original document
$pagecount = $passPdf->setSourceFile($pdfPath);
// copy all pages from the old unprotected pdf in the new one
for ($loop = 1; $loop <= $pagecount; $loop++)
{
$tplidx = $passPdf->importPage($loop);
$passPdf->addPage();
$passPdf->useTemplate($tplidx);
}
$passPdf->SetProtection(['print' => 4, 'copy' => 16], "userpass", "ownerpass");
$passPdf->Output($pdfPath, 'F');
}
我正在使用 CentOS7,我的后端是 PHP v7.0.27 和 Laravel 框架 v4.2。我正在使用 Base64 数据保存 PDF(解码 base64 后)。我想用密码保护和禁用 PDF 编辑。我一直在寻找实现这一目标的方法,但到目前为止还没有成功,我知道这可以通过 Dompdf 实现,但为此需要 HTML 数据来呈现 PDF。如果可能,请告诉我。谢谢!
下面是我用来保存 PDF 的代码
private function decodeBase64ToPdf($fileToReadFrom, $fileToSaveTo)
{
$pdf_base64_handler = fopen($fileToReadFrom,'r'); //Get encoded file content from txt file
$pdf_content = fread($pdf_base64_handler, filesize($fileToReadFrom));
fclose($pdf_base64_handler);
$pdf_decoded = base64_decode($pdf_content); //Decode pdf content
$file = fopen($fileToSaveTo,'w'); //Write decoded data to pdf file
fwrite($file, $pdf_decoded);
if(fclose($file)) //Close output file
{
return true;
}
return false;
}
在测试了很多不同的东西之后。我通过安装 FPDI 和 FPDF 解决了这个问题。 链接: FPDF FPDI
private function decodeBase64ToPdf($fileToReadFrom, $fileToSaveTo)
{
$pdf_base64_handler = fopen($fileToReadFrom,'r'); //Get encoded file content from txt file
$pdf_content = fread($pdf_base64_handler, filesize($fileToReadFrom));
fclose($pdf_base64_handler);
$pdf_decoded = base64_decode($pdf_content); //Decode pdf content
$file = fopen($fileToSaveTo,'w'); //Write decoded data to pdf file
fwrite($file, $pdf_decoded);
if(fclose($file)) //Close output file
{
$this->passwordProtect($fileToSaveTo);
return true;
}
return false;
}
private function passwordProtect($pdfPath)
{
$passPdf = new FpdiProtection();
//calculate the number of pages from the original document
$pagecount = $passPdf->setSourceFile($pdfPath);
// copy all pages from the old unprotected pdf in the new one
for ($loop = 1; $loop <= $pagecount; $loop++)
{
$tplidx = $passPdf->importPage($loop);
$passPdf->addPage();
$passPdf->useTemplate($tplidx);
}
$passPdf->SetProtection(['print' => 4, 'copy' => 16], "userpass", "ownerpass");
$passPdf->Output($pdfPath, 'F');
}