Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

Thanks for your posting, I could solve my orientation problem within a minute!

Anyway, you do not need the special FPDI_"Protection" package for the orientation change alone, a working solution only needs the "FPDI" package (for the getTemplatesize function). Here is a link to the solution from August 2012: FPDF / FPDI addPage() Orientation

In my fpdf_tpl.php (1.6.1) the method to add pages is called AddPage not addPage. Take a look that if you call addPage the method isn't executed and you can't change the orientation.

To use getTemplateSize you only need setasign/fpdi package. For the current version of this package (v2.3.6), the height and width keys are not 'w' and 'h' anymore but 'width' and 'height':

(from setasign\Fpdi\Fpdi.php)

/**
 * Get the size of an imported page or template.
 *
 * Give only one of the size parameters (width, height) to calculate the other one automatically in view to the
 * aspect ratio.
 *
 * @param mixed $tpl The template id
 * @param float|int|null $width The width.
 * @param float|int|null $height The height.
 * @return array|bool An array with following keys: width, height, 0 (=width), 1 (=height), orientation (L or P)
 */
public function getTemplateSize($tpl, $width = null, $height = null)
{
    ...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top