Password Protect PDF in PHP using mPDF

Creating PDF in PHP is a very common requirement in web apps. mPDF is a great library for creating PDF from any HTML using PHP. Please check the article posted earlier to see how we can use mPDF for generating PDF from HTML. But sometimes we need to password protect the PDF file. mPDF also provides a very simple solution to achieve this.

mPDF has a method called SetProtection() which can be used to protect your PDF in several way including password protection, copy/print protection.

First argument is $permissions which is used to allow permissions like copy, print, modify etc. If null or empty array is passed to the argument user will be able to open the pdf but won’t be able to do anything like copy or print.

Second argument is the $user_password which you can set to make the PDF password protected.

Example

Here is a working example for password protecting the PDF generated using mPDF in PHP.

Hope this will help you in making you dev process easier and make secure PDFs from PHP applications.