Last active
October 12, 2017 10:58
-
-
Save motsmanish/98c8c1aae9e59cea6fd76387f55b33bd to your computer and use it in GitHub Desktop.
PHP plugin / library / repository for Password Protecting PDF files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Installation: | |
composer require madnh/fpdi-protection | |
composer require setasign/fpdf:1.8 | |
composer require setasign/fpdi:1.6.2 | |
*/ | |
//PHP function to implement the basic password protection, code is self explanatory | |
```php | |
public function pdfEncrypt($origFile, $destFile, $password_user, $password_owner) | |
{ | |
class_exists('TCPDF', true); // trigger Composers autoloader to load the TCPDF class | |
$pdf = new \FPDI_Protection(); | |
//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(0, $password_user, $password_owner); | |
$pdf->Output($destFile, "F"); | |
return $destFile; | |
} | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment