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
1 2 3 4 5 6 7 8 |
CREATE TABLE `mobiles` ( `id` int(11) NOT NULL, `model_no` varchar(30) NOT NULL, `mobile_name` varchar(30) NOT NULL, `company` varchar(40) NOT NULL, `mobile_category` text NOT NULL, `price` double(16,2) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; |
Step 3:- Now dump some data for table “mobiles” using bellow SQL code
1 2 3 4 5 6 7 8 9 |
INSERT INTO `mobiles` (`id`, `model_no`, `mobile_name`, `company`, `mobile_category`, `price`) VALUES (13, 'SM-G615FZKUINS', 'Samsung Galaxy On Max (Black, ', 'Samsung', 'Smartphones', 20800.00), (14, ' SM-G955FZKGINS', 'Samsung Galaxy S8 Plus (Midnig', 'Samsung', 'Smartphones', 18300.00), (15, 'MN0X2HN/A', 'Apple iPhone 6s (Silver, 32 GB', 'Apple', 'Smartphones', 50000.00), (16, 'MQ8E2HN/A', 'Apple iPhone 8 Plus (Silver, 6', 'Apple', 'Smartphones', 60200.00), (17, ' R1 R829', 'OPPO R1 R829 (Black, 16 GB) (', 'OPPO', 'Smartphones', 19000.00), (18, 'F1', 'OPPO F1 (Gold, 16 GB) (3 GB R', 'OPPO', 'Smartphones', 15500.00), (19, 'MZB5602IN', 'Redmi 4A (Gold, 32 GB) (3 GB ', 'Xiomi', 'Smartphones', 5999.00), (20, 'MZB5653IN', 'Mi A1', 'Xiomi', 'Smartphones', 17500.00); |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); class m_pdf { function m_pdf() { $CI = & get_instance(); log_message('Debug', 'mPDF class is loaded.'); } function load($param=NULL) { include_once APPPATH.'/third_party/mpdf/mpdf.php'; if ($params == NULL) { $param = '"en-GB-x","A4","","",10,10,10,10,6,3'; } return new mPDF(); } } |
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.
1 2 3 4 5 6 |
// construct public function __construct() { parent::__construct(); // load model $this->load->model('Pdf', 'pdf'); } |
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.
1 2 3 4 5 6 7 8 9 |
public function index() { $data['page'] = 'export-pdf'; $data['title'] = 'Export PDF data | Web Preparations'; $data['mobiledata'] = $this->pdf->mobileList(); // load view file for output $this->load->view('header'); $this->load->view('pdf/pdf', $data); $this->load->view('footer'); } |
Step 8:- Now we need to create methodsave_pdf()
, this function used to create a PDF file and show records from MySQL database.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
// for generate pdf public function save_pdf() { //load mPDF library $this->load->library('m_pdf'); //now pass the data// $data['mobiledata'] = $this->pdf->mobileList(); $html=$this->load->view('pdf/pdf',$data, true); //load the pdf.php by passing our data and get all data in $html varriable. $pdfFilePath ="webpreparations-".time().".pdf"; //actually, you can pass mPDF parameter on this load() function $pdf = $this->m_pdf->load(); //generate the PDF! $stylesheet = '<style>'.file_get_contents('assets/css/bootstrap.min.css').'</style>'; // apply external css $pdf->WriteHTML($stylesheet,1); $pdf->WriteHTML($html,2); //offer it to user via browser download! (The PDF won't be saved on your server HDD) $pdf->Output($pdfFilePath, "D"); exit; } |
Step 9:- Now here is a Complete code of Pdfs.php Controller, create your Controller file like Pdfs.php and paste below code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
<?php /** * Description of Pdfs Controller * * @author Web Preparations Team * * @email webpreparations@gmail.com */ if (!defined('BASEPATH')) exit('No direct script access allowed'); class Pdfs extends CI_Controller { // construct public function __construct() { parent::__construct(); // load model $this->load->model('Pdf', 'pdf'); } // show mobiles data public function index() { $data['page'] = 'export-pdf'; $data['title'] = 'Export PDF data | Web Preparations'; $data['mobiledata'] = $this->pdf->mobileList(); // load view file for output $this->load->view('header'); $this->load->view('pdf/pdf', $data); $this->load->view('footer'); } // for generate pdf public function save_pdf() { //load mPDF library $this->load->library('m_pdf'); //now pass the data// $data['mobiledata'] = $this->pdf->mobileList(); $html=$this->load->view('pdf/pdf',$data, true); //load the pdf.php by passing our data and get all data in $html varriable. $pdfFilePath ="webpreparations-".time().".pdf"; //actually, you can pass mPDF parameter on this load() function $pdf = $this->m_pdf->load(); //generate the PDF! $stylesheet = '<style>'.file_get_contents('assets/css/bootstrap.min.css').'</style>'; // apply external css $pdf->WriteHTML($stylesheet,1); $pdf->WriteHTML($html,2); //offer it to user via browser download! (The PDF won't be saved on your server HDD) $pdf->Output($pdfFilePath, "D"); exit; } } ?> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?php /** * Description of Pdf Model * * @author Web Preparations Team * * @email webpreparations@gmail.com */ if (!defined('BASEPATH')) exit('No direct script access allowed'); class Pdf extends CI_Model { // get mobiles list public function mobileList() { $this->db->select(array('m.id', 'm.model_no', 'm.mobile_name', 'm.company', 'm.price', 'm.mobile_category')); $this->db->from('mobiles as m'); $query = $this->db->get(); return $query->result_array(); } } ?> |
Step 11:- Now here view code Create pdf/pdf.php(view) file and paste the below code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
<!DOCTYPE html> <html lang="en"> <head> <link rel="stylesheet" href="<?php echo base_url();?>assets/css/style.css"> <link rel="stylesheet" href="<?php echo base_url();?>assets/css/bootstrap.min.css"> </head> <title>How to export data in PDF format in Codeigniter using MPDF Library</title> <body> <div class="table-responsive"> <table class="table table-hover tablesorter pdf_border"> <thead> <tr class="pdf_border"> <th class="header pdf_border">Model No.</th> <th class="header pdf_border">Mobile Name</th> <th class="header pdf_border">Price</th> <th class="header pdf_border">Company</th> <th class="header pdf_border">Category</th> </tr> </thead> <a class="pull-right btn btn-warning btn-large" style="margin-right:40px" href="<?php echo site_url()?>pdfs/save_pdf"><i class="fa fa-file-excel-o"></i> PDF Data</a> <tbody> <?php if (isset($mobiledata) && !empty($mobiledata)) { foreach ($mobiledata as $key => $val) { ?> <tr class="pdf_border"> <td class="pdf_border"><?php echo $val['model_no']; ?></td> <td class="pdf_border"><?php echo $val['mobile_name']; ?></td> <td class="pdf_border"><?php echo $val['price']; ?></td> <td class="pdf_border"><?php echo $val['company']; ?></td> <td class="pdf_border"><?php echo $val['mobile_category']; ?></td> </tr> <?php } } else { ?> <tr> <td colspan="5" class="alert alert-danger">No Records founds</td> </tr> <?php } ?> </tbody> </table> </div> </body> </html> |
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
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
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?
Webpreparations Team August 04, 2018
Hello Dear, Please share your code so that we can find the issue.:)
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.
Webpreparations Team September 14, 2018
Yes, sure
Please email us on
webpreparations@gmail.com
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
Hello,
Yes, Please share your code we will help you.
Webpreparations November 23, 2018
Test
Ritesh October 25, 2018
Hello Sir bootstrap Css Not working for PDF .
Please Help
Webpreparations Team October 25, 2018
Hello,
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
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.
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
Yashwant More January 31, 2019
hey there its working! but unable to convert hindi fonts.