How to generate pdf in Codeigniter using MPDF Library

Dear Reader, let us we know that “How to generate PDF in Codeigniter using MPDF Library“. So in this Tutorial, We Learn Step By Step  How to generate PDF from HTML Process.

PDF stands for  Portable Document Format. PDF is a file format that used to presents documents in a manner independent of application software, hardware and operating system.PDF files are created using Adobe Acrobat, Acrobat Capture, or similar products. To view and use the files, you need the free Acrobat Reader, which you can easily download.

First, we see Snapshots for our Export data to PDF format


Step 1:- First of all you need to create the database like ci_demo

Step 2:- Now create a table “mobiles” in a database using the below SQL code

Step 3:- Now dump some data for table “mobiles” using bellow SQL code

Step 4:- Now we need to download MPDF library here and extract this in application third_party folder as we can see in below snapshot


Step 5:- Now we need to create a library for load MPDF library, create a M_pdf.php file in library folder and paste the below code

Step 6:- Now we will see our controller, so create a Pdfs Controller in your CI application like Pdfs.php(Controller)

First, create a constructor in__construct() this we will load our necessary libraries.

Step 7:- In Pdfs.php Controller first we need to create Index method in this function we will retrieve data from our database(models) and will pass this data to the respective view.

Step 8:- Now we need to create methodsave_pdf(), this function used to create a PDF file and show records from MySQL database.

Step 9:- Now here is a Complete code of Pdfs.php Controller, create your Controller file like Pdfs.php and paste below code

Step 10:- Now we need to create Pdf.php Model and paste below code

Step 11:- Now here view code Create pdf/pdf.php(view) file and paste the below code

Step 12:-  In pdf.php(view) we add some CSS and JS, so download assets folder here 

extract zip file and copy assets folder and paste in your project’s root directory as we can see in below snapshot


Step 13:- Now run your project, so you need to type in browser localhost/your_project_name/pdfs


http://localhost/ci_demo/pdfs then you can see the result as in below snapshot, you can Export data in PDF format by click on PDF Data button


Congratulations you have successfully generate PDF with MySQL data into PDF format in Codeigniter using MPDF library, if you like this post and was helpful for you then share this post on social media and if you have any query then please contact us or comment below, Thanks.



  • rishad August 03, 2018

    hi, css not working?


    • Webpreparations Team August 04, 2018

      Hello Dear, Please share your code so that we can find the issue.:)


  • Ahmed September 14, 2018


    I have tried all the code as copy paste but its not working. Can you please give me your email so I can send you my code to check and guide me where am doing mistakes.
    Thanks in advance.


    • Webpreparations Team September 14, 2018

      Yes, sure
      Please email us on


      • hazelyn m padilla September 20, 2018

        hello is there a posipility to extract the data into my pdf file, this pdf file is a form application, in my database there have a data’s information how can I extract the data to fill out the information in my pdf file?


        • Webpreparations Team September 20, 2018

          Yes, Please share your code we will help you.


          • Webpreparations November 23, 2018


  • Ritesh October 25, 2018

    Hello Sir bootstrap Css Not working for PDF .
    Please Help


    • Webpreparations Team October 25, 2018

      Please use inline CSS.
      Thank you for contact us


  • jhon November 15, 2018

    i am getting this error A PHP Error was encountered

    Severity: Compile Error

    Message: Switch statements may only contain one default clause

    Filename: mpdf/mpdf.php

    Line Number: 1422


    • Webpreparations Team November 16, 2018

      It’s PHP version issue, please change you php version, thank you for contact us.


  • trilok January 16, 2019


    I have tried all the code as copy paste but its not working. Can you please give me your email so I can send you my code to check and guide me where am doing mistakes.
    can you mail me code please
    Thanks in advance.


    • Webpreparations Team January 20, 2019

      Please don’t copy and paste, but also see the video and read the contents for changing some code for testing environment checking.
      Thank you for contacting. and Our Email Id is


  • Yashwant More January 31, 2019

    hey there its working! but unable to convert hindi fonts.


Leave a comment:

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