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.

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:

//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 “excel” class in controller.

$this->load->library('Pdf');
?>


Create a controller file like Createpdf.php inside “application/controllers” folder.

/*
* 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.

/*
* 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.


$this->load->view('templates/header');
?>


CodeIgniter Create PDF with MySQL Example










Last Updated :
Author :








$this->load->view('templates/footer');
?>


Step 6: Create views
Create a views file named file.php inside “application/views/pdf” folder.





Last Updated :
Author :





Demo  [sociallocker] Download[/sociallocker]

Komentar

Postingan Populer