Home >>Codeigniter Tutorial >Convert HTML to PDF in CodeIgniter using Dompdf

Convert HTML to PDF in CodeIgniter using Dompdf

Convert HTML to PDF in CodeIgniter using Dompdf

PDF is the most utilized arrangement to make the archive in the web application. PDF record gives a straightforward and easy to use approach to download the pack of information in a document. Before download the website page content, the substance should be changed over from HTML to PDF. The HTML to PDF change can be effectively done utilizing PHP library.

Dompdf is a PHP library that assists with creating PDF from HTML content. It's anything but difficult to change over HTML to PDF in PHP with Dompdf. In the event that your application worked with CodeIgniter, a PDF library should be made to produce PDF utilizing Dompdf. Right now, will tell you the best way to change over HTML to pdf and produce PDF utilizing Dompdf in CodeIgniter.

Database table structure(tbl_users)

CREATE TABLE IF NOT EXISTS `tbl_users` (
  `UserID` int(11) NOT NULL primary key auto_increment,
  `Name` varchar(250) NOT NULL,
  `Address` text NOT NULL,
  `City` varchar(250) NOT NULL,
  `PostalCode` varchar(30) NOT NULL,
  `Country` varchar(100) NOT NULL,
  `images` varchar(255) NOT NULL 
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Controller(application/controllers/PdfController.php)


<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class PdfController extends CI_Controller 
{
	public function __construct()
	{
		parent::__construct();
		$this->load->model('PdfModel');
		$this->load->library('pdf');
	}

	public function index()
	{
		$data['customer_data'] = $this->PdfModel->showRecord();
		$this->load->view('htmltopdf', $data);
	}

	public function details()
	{
		if($this->uri->segment(3))
		{
			$customer_id = $this->uri->segment(3);
			$data['customer_details'] = $this->PdfModel->show_single_details($customer_id);
			$this->load->view('htmltopdf', $data);
		}
	}

	public function pdfdetails()
	{
		if($this->uri->segment(3))
		{
			$customer_id = $this->uri->segment(3);
			$html_content = '<h3 align="center">Generate PDF using Dompdf</h3>';
			$html_content .= $this->PdfModel->show_single_details($customer_id);
			$this->pdf->loadHtml($html_content);
			$this->pdf->render();
			$this->pdf->stream("".$customer_id.".pdf", array("Attachment"=>0));
		}
	}

}
?>

Model(application/models/PdfModel.php)


<?php
class PdfModel extends CI_Model
{
	function showRecord()
	{
		return $this->db->get('tbl_users');
	}
	function show_single_details($customer_id)
	{
		$this->db->where('UserID', $customer_id);
		$data = $this->db->get('tbl_users');
		$output = '<table width="100%" cellspacing="5" cellpadding="5">';
		foreach($data->result() as $row)
		{
			$output .= '
			<tr>
				<td width="25%"><img src="'.base_url().'images/'.$row->images.'" /></td>
				<td width="75%">
					<p><b>Name : </b>'.$row->Name.'</p>
					<p><b>Address : </b>'.$row->Address.'</p>
					<p><b>City : </b>'.$row->City.'</p>
					<p><b>Postal Code : </b>'.$row->PostalCode.'</p>
					<p><b>Country : </b> '.$row->Country.' </p>
				</td>
			</tr>
			';
		}
		$output .= '</table>';
		return $output;
	}
}
?>

View(application/views/htmltopdf.php)


<html>
<head>
    <title>Generate PDF using Dompdf</title>
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
</head>
<body>
	<div class="container box">
		<h2 class="text-center text-primary">Generate PDF using Dompdf</h3>
		<br />
		<?php
		if(isset($customer_data))
		{
		?>
			<table class="table table-bordered table-striped">
				<tr>
					<th>ID</th>
					<th>Name</th>
					<th>View</th>
					<th>View in PDF</th>
				</tr>
			<?php
			foreach($customer_data->result() as $row)
			{
				echo '
				<tr>
					<td>'.$row->UserID.'</td>
					<td>'.$row->Name.'</td>
					<td><a href="'.base_url().'index.php/pdfcontroller/details/'.$row->UserID.'">View</a></td>
					<td><a href="'.base_url().'index.php/pdfcontroller/pdfdetails/'.$row->UserID.'">View in PDF</a></td>
				</tr>
				';
			}
			?>
			</table>
		<?php
		}
		if(isset($customer_details))
		{
			echo $customer_details;
		}
		?>
	</div>
</body>
</html>


Library(application/libraries/Pdf.php)


<?php 
error_reporting(1);
if (!defined('BASEPATH')) exit('No direct script access allowed');  
 
require_once 'dompdf/autoload.inc.php';

use Dompdf\Dompdf;

class Pdf extends Dompdf
{
	public function __construct()
	{
		 parent::__construct();
	} 
}

?>

Output(Display All Records)
Output(Convert into pdf)

Total Downloads : 409

Login / Register To Download

No Sidebar ads