Generate PDF from View using DomPDF in CodeIgniter 3

On 3/19/2018

Hi, in this post let's see how to generate pdf from view using dompdf in codeigniter 3. When it comes to generating reports, the pdf format is the most popular in use. In addition, many websites offer downloadable documents, forms and files in pdf format by converting from html to pdf. There are several add-ons and libraries to aid with the process, such as DOMPDF, MPDF, FPDF, TCPDF, Html2Pdf etc. In this case, I will use the DomPDF library to generate PDF documents in code igniter.

DomPDF renders the html layout to a PDF file and supports the loading of external style sheets and inline css styles when creating pdf.

codeigniter generate pdf from view dompdf

How to Generate PDF from CodeIgniter View?

Here we are going to see how to generate pdf document from a codeigniter view file. Basically dompdf reads the html content, create pdf from html and writes it to the output stream.

The following are the steps to implement PDF generation in code igniter.

STEP-1) First download dompdf library, then extract and move the folder to codeigniter's 'application/library' folder.

STEP-2) Next create a custom code igniter library to create pdf using the dompdf class. Create the file 'pdf.php' inside 'application/library' and copy the below contents into the file.

Pdf.php

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

require_once(dirname(__FILE__) . '/dompdf/autoload.inc.php');

class Pdf
{
    function createPDF($html, $filename='', $download=TRUE, $paper='A4', $orientation='portrait'){
        $dompdf = new Dompdf\DOMPDF();
        $dompdf->load_html($html);
        $dompdf->set_paper($paper, $orientation);
        $dompdf->render();
        if($download)
            $dompdf->stream($filename.'.pdf', array('Attachment' => 1));
        else
            $dompdf->stream($filename.'.pdf', array('Attachment' => 0));
    }
}
?>

In the above class, the function createPDF() converts raw html data into pdf document. It allows you to pass five different parameters to control the way in which the pdf is generated. The first parameter is mandatory and the rest are optional.

By default, the function will create downloadable pdf. If you want to preview the file before downloading it, you must set $download=FALSE.

STEP-3) Then you need the codeigniter view file. This view contains a simple html table which has to be converted into a pdf document.

Create 'GeneratePdfView.php' inside 'application/views' folder and copy the contents below into it.

GeneratePdfView.php

<!DOCTYPE html>
<html>
<head>
    <meta content="width=device-width, initial-scale=1.0" name="viewport">
    <meta charset="utf-8">
    <title>Create PDF from View in CodeIgniter Example</title>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" type="text/css" rel="stylesheet" />
</head>
<body>
<h1 class="text-center bg-info">Generate PDF from View using DomPDF</h1>
<table class="table table-striped table-hover">
    <thead>
        <tr>
            <th>#</th>
            <th>Book Name</th>
            <th>Author</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>1</td>
            <td>PHP and MySQL for Dynamic Web Sites</td>
            <td>Larry Ullman</td>
        </tr>
        <tr>
            <td>2</td>
            <td>Pro MEAN Stack Development</td>
            <td>Elad Elrom</td>
        </tr>
        <tr>
            <td>3</td>
            <td>Restful API Design</td>
            <td>Matthias Biehl</td>
        </tr>
        <tr>
            <td>4</td>
            <td>Pro PHP MVC</td>
            <td>Chris Pitt</td>
        </tr>
        <tr>
            <td>5</td>
            <td>Mastering Spring MVC 4</td>
            <td>Geoffroy Warin</td>
        </tr>
        <tbody>
</table>
</body>
</html>

STEP-4) Finally you need a code igniter controller file. Create 'GeneratePdfController.php' inside 'application/controllers'. In the index() function, load the 'pdf' library and convert the view into a pdf file.

GeneratePdfController.php

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

class GeneratePdfController extends CI_Controller {

    function index()
    {
        $this->load->library('pdf');
        $html = $this->load->view('GeneratePdfView', [], true);
        $this->pdf->createPDF($html, 'mypdf', false);
    }
}
?>

That's it. We have all the required files in place. Run the controller and you can see the preview of the newly created pdf document on the browser.

convert html to pdf codeigniter

To simply download the pdf file on load use,

$this->pdf->createPDF($html, 'mypdf');
Read Also:

I hope you now understand better the generation of PDF from the codeigniter view. If you are using composer, just auto load the './vendor/autoload.php' file and you are good to go from there. Hope you find this tutorial useful. Please share it on your social circle if you like it.

No comments:

Post a Comment

Contact Form

Name

Email *

Message *