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.

Generate PDF from HTML code in PHP using mPDF

Recently I came across a requirement where I need to generate a pdf file in PHP. There are several PHP libraries which can be used to generate PDF. But after a little research I found an interesting library, mPDF which can convert HTML code to PDF keeping the structure.

mPDF is a smart library which consider the CSS attached to the HTML.  Not only CSS, mPDF takes care of almost all the HTML tags like, form tags, tables, images, lists etc..

Here is a basic example of using mPDF to generate PDF for a simple HTML page.

Wondering only this much? Yes, it is as simple as that, include the library, create the object and invoke the WriteHTML function passing the HTML you want to convert into PDF. Output function will render the generated PDF in the browser.

Here is another example in which we can see the mPDF consifering the CSS along with the HTML

And here is one more example in which you can email the PDF generated using mPDF directly.

More detailed examples are given here. I hope this helps you some where in your development.

If you need any kind of assistance regarding generating PDF using mPDF, I am happy to invest my time for you, Feel free drop a comment.