07 Oct, 2017

How to generate pdf file with php

07 Oct, 2017

Dear Reader, here we are going to give you the way to “Generate PDF File using PHP”.

Portable Document Format (PDF) is a file format used to represent documents in a manner independent of application software, hardware, and operating system. Almost everyone has a pdf reader software on their PC or mobiles so sharing documents becomes very easy with pdf files. In this tutorial, we will take you to the tour of how to generate a pdf file with php.

Where do I use this script?

Take a situation where you have created a school/college/employee (any other) Management System and there comes a point where you have to generate reports in pdf formats. Or most commonly you have an e-commerce site and you need to generate invoices in pdf format. The situation may be different but the requirement is the same. So this script can prove very handy at that time.

Let’s generate a simple pdf file

The library that I will be using is mpdf which generates pdf files from HTML. You can visit the mpdf official site here for more info.

Follow these three steps to generate a quick pdf file.

  • Download the latest zip file from the mpdf official site( click here ).
  • Extract the zip file to your server and rename it to mpdf (you can give any other name)
  • Attach the class file to your file and write down the script given below.

Features provide by mpdf library 5.7

  • Accept UTF-8 encoded.
  • Right-to-left languages are supported.
  • Bookmarks and Meta tag information are supported in all character sets.
  • A single stylesheet can be used for all pages.
  • Table cell padding and borders are supported.
  • Text-indent for 1st line of the paragraph.
  • List indenting can be defined.
  • Table of contents can be generated automatically.
  • Watermark can be done.
  • Intelligent word-wrapping.
  • And many more

Commonly used methods of mpdf:

Add a stylesheet to the document.

Select your paper size

Adjust page margins

Setting the page orientation, landscape or portrait.

Summing up the above we have 11 parameters that can be passed in the constructor.

Adding Watermarks to the page

Set the Header and Footer text
To know more about mpdf class and methods get it from the manual here.



  • particle October 19, 2017

    Hey! I know this is sort of off-topic however I needed to
    ask. Does running a well-established website like yours take a large amount of work?

    I’m brand new to blogging but I do write in my diary everyday.

    I’d like to start a blog so I will be able to share my
    experience and views online. Please let me know if you
    have any suggestions or tips for brand new aspiring bloggers.



    • Webpreparations Team October 21, 2017

      Hi, first of all, wanna say thanks a lot for appreciating our website’s contents. We love to share our knowledge with readers like you. In the same way you can also share your views with needy people. And now for your queries, want to say that, Off course for doing any thing bigger, needs work hard, but if we are passionate about our work, then we will reach our goal definitely. Just move on n on and you will get the result for sure.


Leave a comment:

Your email address will not be published. Required fields are marked *