Create PDF in Codeigniter using Dompdf – flexible library

Create PDF in Codeigniter using Dompdf – flexible library

How to generate a PDF in Codeigniter using Dompdf ?

In this tutorial we will create pdf in codeigniter using Dompdf. Here I used image, inline css, external css and internal css to get you know how these things can be used to create pdf.

To create Pdf in codeigniter Dompdf is the most flexible library to use images and External stylesheet.

Dompdf is a style-driven renderer with a CSS 2.1 compliant HTML layout and rendering engine written in PHP. It will download and read external stylesheets, inline style tags, and the style attributes of individual HTML elements. It also supports most presentational HTML attributes.

Dompdf consist of so many features like it handles most CSS 2.1 and a few CSS3 properties, including @import, @media & @page rules, Supports external stylesheets, either local or through http/ftp, image support,basic SVG support, etc. For more features and its requirements please visit Dompdf 

As its rendering engine written in php, we can use this library to generate pdf in codeigniter by following the below steps.

Follow the steps below to create your PDF.

Step 1 :- Click here to download Dompdf. Extract and paste dompdf folder in libraries

Or we can use the composer to install Dompdf, for that run the following command

composer require dompdf/dompdf

After installing composer, please make sure the autoload file from Composer is loaded.

require 'vendor/autoload.php';

Step 2 :- Create a library file in libraries Pdf.php

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

use Dompdf\Dompdf;

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

?>

Step 3 :- Create controller file in controllers PdfDownload.php

<?php
class PdfDownload extends CI_Controller
{

	public function __construct()
	{
	 	parent::__construct();
	}

	public function index()
	{
		$html = '<!DOCTYPE html>
		<html lang="en">
		<head>
		<meta charset="utf-8">
		<title>Example 1</title>
		<link href="themes/home/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
		<link rel="stylesheet" href="themes/invoiceStyle.css" media="all" />
		<style>
		.invoice-title h2, .invoice-title h3 {
    display: inline-block;
}

.table > tbody > tr > .no-line {
    border-top: none;
}

.table > thead > tr > .no-line {
    border-bottom: none;
}

.table > tbody > tr > .thick-line {
    border-top: 2px solid;
}
.pull-right
{
	float:right;
}
</style>
		</head>
		<body>
		<header class="clearfix">
		<div id="logo">
		<img src="themes/logo/companylogo.jpeg" width="200">
		</div>
		<div class="container">
    <div class="row">
        <div class="col-xs-12">
        	<div class="invoice-title">
    			<h2 class="pull-left">Invoice</h2><h3 class="pull-right">Order Id # hxs1234567</h3>
    		</div>
    		<hr>
    		<div class="row">
    			<div class="col-xs-6">
    				<address>
    				<strong>Billed To:</strong><br>
    				        Jeenal Yatishkumar Bhatt<br>
    				        	jbhatt@healthxapp.com
    				</address>
    			</div>
    			<div class="col-xs-6 text-right">
    				<address>
        			<strong>Shipped To:</strong><br>
    					206/3 Signet hub near akshar chowk <br>
    					Vadodara, Gujratn<br>
    					India, 390023
    				</address>
    			</div>
    		</div>
    		<div class="row">
    			<div class="col-xs-6">
    				<address>
    					<strong>Payment Method:</strong><br>
    					<br>
    				
    				</address>
    			</div>
    			<div class="col-xs-6 text-right">
    				<address>
    					<strong>Order Date:</strong><br>
    					March 7th, 2018<br><br>
    				</address>
    			</div>
    		</div>
    	</div>
    </div>
    
    <div class="row">
    	<div class="col-md-12">
    		<div class="panel panel-default">
    			<div class="panel-heading">
    				<h3 class="panel-title"><strong>Order summary</strong></h3>
    			</div>
    			<div class="panel-body">
    				<div class="table-responsive">
    					<table class="table table-condensed">
    						<thead>
                                <tr>
        							<td><strong>Medication</strong></td>
        							<td class="text-center"><strong>Strength</strong></td>
        							<td class="text-center"><strong>Quantity</strong></td>
        							<td class="text-right"><strong>Price</strong></td>
                                </tr>
    						</thead>
    						<tbody>
    							<!-- foreach ($order->lineItems as $line) or some such thing here -->
    							<tr>
    								<td>Crosin</td>
    								<td class="text-center">100 mg</td>
    								<td class="text-center">10</td>
    								<td class="text-right">30 INR</td>
    							</tr>
                                <tr>
        							<td>Paracetamol</td>
    								<td class="text-center">20 mg</td>
    								<td class="text-center">3</td>
    								<td class="text-right">50 INR</td>
    							</tr>
    							<tr>
    								<td class="thick-line"></td>
    								<td class="thick-line"></td>
    								<td class="thick-line text-center"><strong>Subtotal</strong></td>
    								<td class="thick-line text-right">80 INR</td>
    							</tr>
    							<tr>
    								<td class="no-line"></td>
    								<td class="no-line"></td>
    								<td class="no-line text-center"><strong>Shipping</strong></td>
    								<td class="no-line text-right">15 INR</td>
    							</tr>
    							<tr>
    								<td class="no-line"></td>
    								<td class="no-line"></td>
    								<td class="no-line text-center"><strong>Total</strong></td>
    								<td class="no-line text-right">95 INR</td>
    							</tr>
    						</tbody>
    					</table>
    				</div>
    			</div>
    		</div>
    	</div>
    </div>
</div>
		</body>
		</html>';
		$filename = "newpdffile";

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

		$dompdf = new Pdf();

		$dompdf->loadHtml($html);

	        // (Optional) Setup the paper size and orientation
		$dompdf->setPaper('A4', 'portrait');

		// Render the HTML as PDF
		$dompdf->render();

		// Output the generated PDF to Browser
		$dompdf->stream($filename,array("Attachment"=>0));

	}

}

Done..! you can run the controller and there your pdf. http://localhost/test/PdfDownload/

** you can change portrait to landscape

** A4 size can change to your required paper size

** Remove ,array(“Attachment”=>0) from last step and just specify $filename to direct download pdf ie, $dompdf->stream($filename);

For your doubts and queries about generating a pdf in codeigniter using Dompdf please comment below.

For other codeigniter related solutions please visit Codeigniter

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top