Password protect a PDF document in PHP

The following program will create password protected PDF documents in the fly using PHP. You need to download the required library files from here. Also you could get more information from here.

PHP Code

error_reporting( E_ALL ^ E_DEPRECATED );

function pdfEncrypt ($origFile, $password, $destFile){
     $pdf =& new FPDI_Protection();
// set the format of the destinaton file
     $pdf->FPDF("P", "in", array('8.27','11.69'));
//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);

return $destFile;

//password for the pdf file
$password = "a2zwebhelp";

//name of the original file
$origFile = "AuditReport.pdf";

//name of the destination file 
$destFile ="AuditReport_p.pdf";

//encrypt the book and create the protected file
pdfEncrypt($origFile, $password, $destFile );


Download Code Total Downloads: 2012
comments powered by Disqus