I am using the FPDF() method (from FPDI_Protection.php) to import existing PDFs and apply password protection.
The problem I'm having is that the original PDF has a mix of portrait and landscape pages (8.5"X11" & 11"X8.5"), whereas the import method makes you define it once. I can define the newly created pdf to be 11"X11", which fixes the problem of one of the orientations cropping, but this is not ideal for printing purposes, as the PDF is scaled and left aligned, causing poor readability/printout.
Is there any sort of routine I could use, as the original document is being looped through, to detect the original size and set the new page orientation on the fly?
function pdfEncrypt ($origFile, $password, $destFile) // RESPONSIBLE FOR ADDING PASSWORD PROTECTION TO PDF FILES
{
require_once('fpdi/FPDI_Protection.php');
$pdf = new FPDI_Protection();
// set the format of the destinaton file, in our case 6×9 inch
$pdf->FPDF('P', 'in', array('11','11'));
//calculate the number of pages from the original document
$pagecount = $pdf->setSourceFile($origFile);
// copy all pages from the old unprotected pdf in the new one
for ($loop = 1; $loop <= $pagecount; $loop++)
{
$tplidx = $pdf->importPage($loop);
$pdf->addPage();
$pdf->useTemplate($tplidx);
}
// protect the new pdf file, and allow no printing, copy etc and leave only reading allowed
$pdf->SetProtection(array('print'), $password, '');
$pdf->Output($destFile, 'F');
return $destFile;
}
Or, alternatively, is there a simpler way to add a password to an existing pdf using php?
Ok, I pulled my hair out for days on this one. After tireless googling every iteration of terms related to my problem I was able to find one instance of a solution that actually worked (I tried installing pdflib lite, phpinfo, ghostscript, xpdf, etc. etc., to measure dimensions to no avail). What worked was this (you need the FPDI_Protection package [free]):
$specs = $pdf->getTemplateSize($tplidx);
$pdf->addPage($specs['h'] > $specs['w'] ? 'P' : 'L');
The full function is as follows:
function pdfEncrypt ($origFile, $password, $destFile) // RESPONSIBLE FOR ADDING PASSWORD PROTECTION TO PDF FILES
{
require_once('fpdi/FPDI_Protection.php');
$pdf = new FPDI_Protection();
// set the format of the destinaton file
$pdf->FPDF('P', 'in', array('8.5','11'));
//calculate the number of pages from the original document
$pagecount = $pdf->setSourceFile($origFile);
// copy all pages from the old unprotected pdf in the new one
for ($loop = 1; $loop <= $pagecount; $loop++)
{
$tplidx = $pdf->importPage($loop);
$specs = $pdf->getTemplateSize($tplidx);
$pdf->addPage($specs['h'] > $specs['w'] ? 'P' : 'L');
$pdf->useTemplate($tplidx);
}
// protect the new pdf file
$pdf->SetProtection(array('print'), $password, '');
$pdf->Output($destFile, 'F');
return $destFile;
}
The addition of those two lines of code was able to detect whether the original page was portrait of landscape, and recreate the page in the output file the same way. Hallelujah.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With