I am having two codes one for editing already uploaded pdf and second one for securing already uploaded pfd with password
here are the code snippets
1) For pdf editing
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) For Password protecting
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 );
Both code works fine. But when I try to combine both of them. either of them never works as code 1 uses newer libraries and code 2 uses older. I tried to work code 2 with new libraries but gives file can not be loaded kinda errors.
I have added code 1's functionality in to code 2 like this:
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 );
This code saves the file as password protected, but does not edit it at all. neither gives any error or warning. What's I am doing wrong??
Please suggest. Thanks
First of all you should update all used classes to their current versions: FPDF, FPDI and FPDI_Protection (see previous link).
After that you simply have to require the needed files and your last script should work as expected:
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);
...