我有两个代码用于编辑已上传的pdf,第二个代码用于保护已上传的带密码的pfd
这里是代码片段
1)用于pdf编辑
require_once('fpdf.php');
require_once('fpdi.php');
$pdf = new FPDI();
$filen="upload/json_tutorial.pdf";
$pageCount = $pdf->setSourceFile($filen);
// iterate through all pages
for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
// import a page
$tplidx = $pdf->importPage($pageNo);
$pdf->addPage();
$pdf->useTemplate($tplidx, 0, 0, 220,270);
$pdf->SetFont('Helvetica');
$pdf->SetTextColor(0, 0, 255);
$pdf->SetXY(5, 5);
$cur_page_no=$pdf->PageNo();
$min=2;
$max=10;
if((($pdf->PageNo())>=$min) && (($pdf->PageNo())<=$max))
{
$author="AuthorName";
$pdf->Cell(320,10,$author,0,0,'C');
}
}
$pdf->Output('newpdf.pdf', 'D');
2)用于密码保护
function pdfEncrypt ($origFile, $password, $destFile)
{
require_once('FPDI_Protection.php');
$pdf =& new FPDI_Protection();
$pagecount = $pdf->setSourceFile($origFile);
for ($loop = 1; $loop <= $pagecount; $loop++)
{
$tplidx = $pdf->importPage($loop);
$pdf->addPage();
$pdf->useTemplate($tplidx);
}
$pdf->SetProtection(array(), $password,'');
$pdf->Output($destFile, 'D');
return $destFile;
}
$password = "pass123";
$origFile = "json_tutorial.pdf";
$destFile ="pd_protected.pdf";
pdfEncrypt($origFile, $password, $destFile );
两个代码都运行正常。但是,当我尝试将它们两者结合起来时。其中任何一个都不会工作,因为代码1使用较新的库,而代码2使用较旧的库。我尝试使用新库来处理代码2,但是文件无法加载有点错误。
我已将代码1的功能添加到代码2中,如下所示:
function pdfEncrypt ($origFile, $password, $destFile)
{
require_once('FPDI_Protection.php');
$pdf =& new FPDI_Protection();
$pagecount = $pdf->setSourceFile($origFile);
for ($pageNo = 1; $pageNo <= $pagecount; $pageNo++)
{
$tplidx = $pdf->importPage($pageNo);
$pdf->addPage();
$pdf->useTemplate($tplidx, 0, 0, 220,270);
$pdf->SetFont('Helvetica');
$pdf->SetTextColor(0, 155, 255);
$pdf->SetXY(5, 5);
$min=2;
$max=10;
if((($pdf->PageNo())>=$min) && (($pdf->PageNo())<=$max))
{
$author="KomalD";
$pdf->Cell(320,10,$author,0,0,'C');
}
}
$pdf->SetProtection(array(), $password,'');
$pdf->Output($destFile, 'D');
return $destFile;
}
$password = "pass123";
$origFile = "json_tutorial.pdf";
$destFile ="pd_protected.pdf";
pdfEncrypt($origFile, $password, $destFile );
此代码将文件保存为受密码保护,但根本不进行编辑。既不会出现任何错误或警告。我做错了什么?
请建议。 感谢
答案 0 :(得分:2)
首先,您应该将所有使用过的类更新为当前版本: FPDF,FPDI和FPDI_Protection(参见上一个链接)。
之后,您只需要需要所需的文件,最后一个脚本应该按预期工作:
function pdfEncrypt ($origFile, $password, $destFile)
{
require_once('fpdf.php');
require_once('fpdi.php');
require_once('FPDI_Protection.php');
$pdf = new FPDI_Protection(); // <-- remove the "&"
$pagecount = $pdf->setSourceFile($origFile);
...