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

how-to-generate-pdf-in-codeigniter-using-MPDF-library-step-1

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

how-to-generate-pdf-in-codeigniter-using-MPDF-library-step-2

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

how-to-generate-pdf-in-codeigniter-using-MPDF-library-step-3

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

like

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

how-to-generate-pdf-in-codeigniter-using-MPDF-library-step-1

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.

«
»

14 Comments:

  • rishad August 03, 2018

    hi, css not working?

    Reply

    • Webpreparations Team August 04, 2018

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

      Reply

  • Ahmed September 14, 2018

    Hi,

    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.

    Reply

    • Webpreparations Team September 14, 2018

      Yes, sure
      Please email us on
      webpreparations@gmail.com

      Reply

      • 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?

        Reply

        • Webpreparations Team September 20, 2018

          Hello,
          Yes, Please share your code we will help you.

          Reply

          • Webpreparations November 23, 2018

            Test

  • Ritesh October 25, 2018

    Hello Sir bootstrap Css Not working for PDF .
    Please Help

    Reply

    • Webpreparations Team October 25, 2018

      Hello,
      Please use inline CSS.
      Thank you for contact us

      Reply

  • 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

    Reply

    • Webpreparations Team November 16, 2018

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

      Reply

  • trilok January 16, 2019

    Hi,

    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
    trilok@active.agency
    Thanks in advance.

    Reply

    • 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 webpreparations@gmail.com

      Reply

  • Yashwant More January 31, 2019

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

    Reply

Leave a comment:

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