Create PDF file using Codeigniter with MYSQL
Portable Document Format. The PDF layout is commonly used for saving documents and publications in a widespread layout that can be considered on more than one devices. In this educational, explains the way to generate a pdf record in Codeigniter the usage of TCPDF library.TCPDF is now one of the world’s maximum active Open Source tasks, used each day via tens of millions of customers and protected in lots of Web packages.
First, we need to down load TCPDF Library, then extract TCPDF Library
Step 1: Extract TCPDF Library
Note: Copy and Paste internal “software/third_party” folder.
Step 2: Create record
Create a document named Pdf.Php inner “utility/libraries” folder.
Step 3: Create Database
For this tutorial, you need a MySQL database with the following table:
<?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 4: Create Controller and cargo class
Syntax: Load “Pdf” elegance in controller.
<?php
// library
$this->load->library('Pdf');
?>
Create a controller document like Createpdf.Php inside “application/controllers” folder.
<?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 version file named Createpdf_model.Personal home page inside “software/fashions” folder.
<?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 perspectives record named index.Php inner “application/views/pdf” folder.
<?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 document named file.Personal home page internal “application/views/pdf” folder.
<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>