In a recent project, I was asked to modify uploaded PDFs to dynamically insert a user specified password to keep files secure. To achieve this functionality, I made use of the PDFI library from Setasign, and in particular, the FpdiProtection class. This tutorial shows how this can be done.
Prerequisites
Using Composer (or by other means), you first need to install the following libraries from Setasign:
setasign/fpdf(v1.8)setasign/fpdi-protection(v2.0)
Installing the above libraries will also install setasign/fpdi.
Creating the Password Helper
To assist with password protecting PDFs, I created a helper Class called PasswordProtectPDF, which you can find below. The protect function handles importing each page from your source PDF (specified as $file). The output function then return the new generated PDF in various formats.
| <?php | |
| require "vendor/autoload.php"; | |
| use setasign\Fpdi\Fpdi; | |
| use setasign\Fpdi\PdfReader; | |
| use setasign\FpdiProtection\FpdiProtection; | |
| class PasswordProtectPDF | |
| { | |
| protected $pdf = null; | |
| public function __construct($file, $user_pass, $master_pass = null) | |
| { | |
| $this->protect($file, $user_pass, $master_pass); | |
| } | |
| public function protect($file, $user_pass, $master_pass = null) | |
| { | |
| $pdf = new FpdiProtection(); | |
| // set the source of the PDF to protect | |
| $pagecount = $pdf->setSourceFile($file); | |
| // copy all pages from the old unprotected pdf in the new one | |
| for ($i = 1; $i <= $pagecount; $i++) { | |
| // import each page from the original PDF | |
| $tplidx = $pdf->importPage($i); | |
| // get the size, orientation etc of each imported page | |
| $specs = $pdf->getTemplateSize($tplidx); | |
| // set the correct orientatil, width and height (allows for mixed page type PDFs) | |
| $pdf->addPage($specs['orientation'], [ $specs['width'], $specs['height'] ]); | |
| $pdf->useTemplate($tplidx); | |
| } | |
| // set user and master passwords, and allowed permissions | |
| // the master password allows the PDF to be unlocked for full access | |
| $pdf->SetProtection([ FpdiProtection::PERM_PRINT, FpdiProtection::PERM_DIGITAL_PRINT ], $user_pass, $master_pass); | |
| // set app name as creator | |
| $pdf->SetCreator("My Awesome App"); | |
| $this->pdf = $pdf; | |
| return $this; | |
| } | |
| public function setTitle($title) | |
| { | |
| // set the title of the document | |
| $this->pdf->SetTitle($title); | |
| return $this; | |
| } | |
| public function output($name, $type = 'I') | |
| { | |
| // output the document | |
| if ($type === 'S') { | |
| return $this->pdf->Output($type, $name); | |
| } else { | |
| $this->pdf->Output($type, $name); | |
| } | |
| return $this; | |
| } | |
| } |
Examples
Below are various examples of how the dynamically generated PDF can be handled, e.g. returning it to the browser or forcing the PDF to download to the user’s device. The following output types are available (via FPDF library):
I= Inline PDFS= PDF as a string for you to handle manually (i.e. with manual headers to force inline rendering or download)D= Forcing the PDF to download to the user’s device, using the second parameter as the filename.F= Saving the PDF to the server as a file, using the second parameter as the file path and name.
| <?php | |
| require "PasswordProtectPDF.php"; | |
| try { | |
| $pdf = new PasswordProtectPDF("test.pdf", "mypassword"); | |
| // render the PDF inline (i.e. within browser where supported) | |
| $pdf->setTitle("Test PDF")->output('I', 'test.pdf'); | |
| } catch (\Exception $e) { | |
| // catch any errors in rendering the PDF | |
| if ($e->getCode() == 267) { | |
| die('The compression on this file is not supported.'); | |
| } | |
| die('There was an error generating the PDF.'); | |
| } | |
| exit; | |
| $pdf = new PasswordProtectPDF("test.pdf", "mypassword"); | |
| // force the PDF to download to filename specified | |
| $pdf->setTitle("Test PDF")->output('D', 'test.pdf'); | |
| exit; | |
| $pdf = new PasswordProtectPDF("test.pdf", "mypassword"); | |
| // save PDF as file on the server | |
| $pdf->setTitle("Test PDF")->output('F', 'test.pdf'); | |
| exit; |
Troubleshooting
The FPDF library may not be able to handle all types of files, for example, depending on the compression used in the source PDF. Wrapping the above examples in a try..catch statement will help you catch any errors in generating the PDF.
The above example (lines 4-14) shows how compression errors can be caught and handled gracefully.
For anything else, feel free to leave a comment below and I’ll try and help.
