Create PDF file using Codeigniter with MYSQL
PDF stands for the Portable Document Format. The PDF format is commonly used for saving documents and publications in a standard format that can be viewed on multiple devices. In this tutorial, explains how to generate a pdf file in Codeigniter using TCPDF library.TCPDF is now one of the world’s most active Open Source projects, used daily by millions of users and included in thousands of Web applications.
First, we need to download TCPDF Library, then extract TCPDF Library
Step 1: Extract TCPDF Library
Note: Copy and Paste inside “application/third_party” folder.
Step 2: Create file
Create a file named Pdf.php inside “application/libraries” folder.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); /* * ======================================= * Author : TechArise Team * License : Protected * Email : info@techarise.com * * Dilarang merubah, mengganti dan mendistribusikan * ulang tanpa sepengetahuan Author * ======================================= */ require_once APPPATH . "/third_party/tcpdf/tcpdf.php"; class Pdf extends tcpdf { public function __construct() { parent::__construct(); } } ?> |
Step 3: Create Database
For this tutorial, you need a MySQL database with the following table:
1 2 3 4 5 6 7 8 9 10 |
<?php //Table structure for table `blog` CREATE TABLE `blog` ( `name` varchar(255) DEFAULT NULL, `description` longtext, `created_date` varchar(12) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; //Dumping data for table `blog` INSERT INTO `blog` (`name`, `description`, `created_date`) VALUES ('TechArise Team', 'Demo text', '1524380830'); ?> |
Step 4: Create Controller and load class
Syntax: Load “Pdf” class in controller.
1 2 3 4 |
<?php // library $this->load->library('Pdf'); ?> |
Create a controller file like Createpdf.php inside “application/controllers” folder.
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
<?php /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * @package Createpdf : CodeIgniter Create PDF * * @author TechArise Team * * @email info@techarise.com * * Description of Createpdf Controller */ if (!defined('BASEPATH')) exit('No direct script access allowed'); class Createpdf extends CI_Controller { public function __construct() { parent::__construct(); $this->load->model('Createpdf_model', 'createpdf'); } public function index() { $data['title'] = 'Create PDF | TechArise'; $data['getInfo'] = $this->createpdf->getContent(); $this->load->view('pdf/index', $data); } // generate PDF File public function generatePDFFile() { $data = array(); $htmlContent=''; $data['getInfo'] = $this->createpdf->getContent(); $htmlContent = $this->load->view('pdf/file', $data, TRUE); $createPDFFile = time().'.pdf'; $this->createPDF(ROOT_FILE_PATH.$createPDFFile, $htmlContent); redirect(HTTP_FILE_PATH.$createPDFFile); } // create pdf file public function createPDF($fileName,$html) { ob_start(); // Include the main TCPDF library (search for installation path). $this->load->library('Pdf'); // create new PDF document $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); // set document information $pdf->SetCreator(PDF_CREATOR); $pdf->SetAuthor('TechArise'); $pdf->SetTitle('TechArise'); $pdf->SetSubject('TechArise'); $pdf->SetKeywords('TechArise'); // set default header data $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING); // set header and footer fonts $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN)); $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA)); $pdf->SetPrintHeader(false); $pdf->SetPrintFooter(false); // set default monospaced font $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED); // set margins $pdf->SetMargins(PDF_MARGIN_LEFT, 0, PDF_MARGIN_RIGHT); $pdf->SetHeaderMargin(0); $pdf->SetFooterMargin(0); // set auto page breaks //$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM); $pdf->SetAutoPageBreak(TRUE, 0); // set image scale factor $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO); // set some language-dependent strings (optional) if (@file_exists(dirname(__FILE__).'/lang/eng.php')) { require_once(dirname(__FILE__).'/lang/eng.php'); $pdf->setLanguageArray($l); } // set font $pdf->SetFont('dejavusans', '', 10); // add a page $pdf->AddPage(); // output the HTML content $pdf->writeHTML($html, true, false, true, false, ''); // reset pointer to the last page $pdf->lastPage(); ob_end_clean(); //Close and output PDF document $pdf->Output($fileName, 'F'); } } ?> |
Step 5: Create Model
Create a model file named Createpdf_model.php inside “application/models” folder.
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 |
<?php /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * Description of Createpdf Model: CodeIgniter Createpdf MySQL * * @author TechArise Team * * @email info@techarise.com */ if (!defined('BASEPATH')) exit('No direct script access allowed'); class Createpdf_model extends CI_Model { // get content public function getContent() { $this->db->select(array('b.name', 'b.description', 'b.created_date')); $this->db->from('blog b'); $query = $this->db->get(); return $query->row_array(); } } ?> |
Step 6: Create views
Create a views file named index.php inside “application/views/pdf” folder.
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 |
<?php $this->load->view('templates/header'); ?> <div class="row"> <div class="col-lg-12"> <h2>CodeIgniter Create PDF with MySQL Example</h2> </div> </div><!-- /.row --> <div class="row"> <div class="col-lg-12"> <a href="<?php site_url();?>generate-pdf-file" target="_blank" class="pull-right btn btn-primary btn-xs" style="margin: 2px;"><i class="fa fa-plus"></i> Create PDF File</a> <a href="#" class="pull-right btn btn-info btn-xs" style="margin: 2px;"><i class="fa fa-mail-reply"></i> Back To Tutorial</a> </div> </div> <hr> <div class="row"> <div class="col-lg-12"> <div> <strong>Last Updated :</strong> <?php print date(DATE_FORMAT_SIMPLE,$getInfo['created_date']);?> <strong>Author :</strong> <?php print $getInfo['name'];?> </div> <?php print $getInfo['description'];?> </div> </div><!-- /.row --> <hr> <div class="row"> <div class="col-lg-12"> <a href="<?php site_url();?>generate-pdf-file" target="_blank" class="pull-right btn btn-primary btn-xs" style="margin: 2px;"><i class="fa fa-plus"></i> Create PDF File</a> <a href="#" class="pull-right btn btn-info btn-xs" style="margin: 2px;"><i class="fa fa-mail-reply"></i> Back To Tutorial</a> </div> </div> <?php $this->load->view('templates/footer'); ?> |
Step 6: Create views
Create a views file named file.php inside “application/views/pdf” folder.
1 2 3 4 5 6 7 8 9 |
<div class="row"> <div class="col-lg-12"> <div> <strong>Last Updated :</strong> <?php print date(DATE_FORMAT_SIMPLE,$getInfo['created_date']);?> <strong>Author :</strong> <?php print $getInfo['name'];?> </div> <?php print $getInfo['description'];?> </div> </div> |