Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to password protect pdf in PHP [duplicate]

Possible Duplicate:
How to password protect an uploaded pdf in PHP

I have a web app where users can upload pdf documents. Is there a php library that I can use to password protect the pdf file? I need the library to preserve all aspects of the original pdf (i.e. size, fonts, resolution, etc).

like image 214
Favourite Onwuemene Avatar asked Dec 15 '22 18:12

Favourite Onwuemene


1 Answers

Download the library used: Protect PDF in PHP

<?php

function pdfEncrypt ($origFile, $password, $destFile){
//include the FPDI protection http://www.setasign.de/products/pdf-php-solutions/fpdi-protection-128/
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('6','9'));

//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(),$password);
$pdf->Output($destFile, 'F');

return $destFile;
}

//password for the pdf file
$password = '[email protected]';

//name of the original file (unprotected)
$origFile = 'book.pdf';

//name of the destination file (password protected and printing rights removed)
$destFile ='book_protected.pdf';

//encrypt the book and create the protected file
pdfEncrypt($origFile, $password, $destFile );
?>
like image 85
Favourite Onwuemene Avatar answered Dec 25 '22 02:12

Favourite Onwuemene