Convert HTML to PDF in CodeIgniter using Dompdf

convert html to pdf

Convert HTML to PDF in CodeIgniter using Dompdf

Nowadays PDF is the most used format in various web applications. There are multiple libraries like TCPDF, Snappy, mPDF etc can be used to create PDF in a web application. DomPDF is one of the most popular libraries to convert your data into PDF. Here are very easy steps to create HTML to PDF:

We are using default views/welcome_message.php with controller/welcome.php to generate pdf that is easy to understand. You can add your model & include it into the controller.

Step 1: Download DOMPDF from here. Paste it into application\libraries folder
Step 2: Create a file name named Pdf.php into application\libraries folder and paste below code.

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

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

Step 3 : Now create an controller that renders html view to PDF.

<?php

if (!defined('BASEPATH'))
        exit('No direct script access allowed');
class Welcome extends CI_Controller {
	function GeneratePdf(){
		$this->load->view('welcome_message');
		$html = $this->output->get_output();
        		// Load pdf library
		$this->load->library('pdf');
		$this->pdf->loadHtml($html);
		$this->pdf->setPaper('A4', 'landscape');
		$this->pdf->render();
		// Output the generated PDF (1 = download and 0 = preview)
		$this->pdf->stream("html_contents.pdf", array("Attachment"=> 0));		
	}
}

Step 4 :In above controller we have used default welcome.php, you can replace with YOUR_VIEW.php file. Create a model if you are using custom view in step4 and load it into controller.

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?><!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Welcome to CodeIgniter</title>
  <style type="text/css">
  ::selection { background-color: #E13300; color: white; }
  ::-moz-selection { background-color: #E13300; color: white; }
  body {
    background-color: #fff;
    margin: 40px;
    font: 13px/20px normal Helvetica, Arial, sans-serif;
    color: #4F5155;
  }
  a {
    color: #003399;
    background-color: transparent;
    font-weight: normal;
  }
  h1 {
    color: #444;
    background-color: transparent;
    border-bottom: 1px solid #D0D0D0;
    font-size: 19px;
    font-weight: normal;
    margin: 0 0 14px 0;
    padding: 14px 15px 10px 15px;
  }
  code {
    font-family: Consolas, Monaco, Courier New, Courier, monospace;
    font-size: 12px;
    background-color: #f9f9f9;
    border: 1px solid #D0D0D0;
    color: #002166;
    display: block;
    margin: 14px 0 14px 0;
    padding: 12px 10px 12px 10px;
  }
  #body {
    margin: 0 15px 0 15px;
  }
  p.footer {
    text-align: right;
    font-size: 11px;
    border-top: 1px solid #D0D0D0;
    line-height: 32px;
    padding: 0 10px 0 10px;
    margin: 20px 0 0 0;
  }
  #container {
    margin: 10px;
    border: 1px solid #D0D0D0;
    box-shadow: 0 0 8px #D0D0D0;
  }
  </style>
</head>
<body>

  <div id="container">
    <h1>Welcome to CodeIgniter!</h1>
    <div id="body">
      <p>The page you are looking at is being generated dynamically by CodeIgniter.</p>
      <p>If you would like to edit this page you'll find it located at:</p>
      <code>application/views/welcome_message.php</code>
      <p>The corresponding controller for this page is found at:</p>
      <code>application/controllers/Welcome.php</code>
      <p>If you are exploring CodeIgniter for the very first time, you should start by reading the <a href="user_guide/">User Guide</a>.</p>
    </div>
    <p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds. <?php echo  (ENVIRONMENT === 'development') ?  'CodeIgniter Version <strong>' . CI_VERSION . '</strong>' : '' ?></p>
  </div>
</body>
</html>

Editor

convert html to pdf

That’s it your rendered html in view file will looks like above image. Check it by http://YOUR_SITE_URL/welcome/generatepdf

So this is How to Convert HTML to PDF in CodeIgniter using Dompdf, If you are looking to create a unique, feature-rich and error-free web application development then you need to hire a dedicated developer from Techcompose Solutions who work solely on your project who will make sure your project is developed within the time frame and it meets your business requirement. Our Developers are experienced and had executed several small to complex development projects. We strictly believe in proper communication at the beginning which makes sure that your requirement is totally understood and we both are in the right track. Our developers know very well what your business may require to enable you to stand out in competition. Contact us to hire dedicated developers for developing a project on Codeigniter or you can reach us at inquiry@techcompose.com