dompdf codeigniter PDF Generation – How to Configure domPdf?

dompdf codeigniter – CodeIgniter customer Library for convert HTML to PDF – Create custom PDF library for generate PDF document in CodeIgniter using Dompdf.

dompdf codeigniter

I would like to present how to use domPdf library with codeigniter to generate PDF reports. and here step by step Convert HTML to PDF in CodeIgniter using Dompdf.

How to Convert HTML to PDF in CodeIgniter using Dompdf Library?

Step 1: Download Fresh Codeigniter 3

download from here : Download Codeigniter 3.

Step 2: Download dompdf library from GitHub
https://github.com/dompdf/dompdf/releases

Load CodeIgniter Pdf library to use Dompdf class.

Read Also:  Laravel 6 Eloquent Join Multiple Tables

“application/libraries”/pdf.php

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

// reference the Dompdf namespace
use Dompdf\Dompdf;

class Pdf
{
    public function __construct(){
        
        require_once dirname(__FILE__).'/dompdf/autoload.inc.php';
        
        $pdf = new DOMPDF();
        
        $CI =& get_instance();
        $CI->dompdf = $pdf;
        
    }
}
?>

Convert HTML content and generate PDF using Dompdf.

Step 3: Add Controller Method

application/controllers/Welcome.php

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


class Welcome extends CI_Controller {

   public function index()
   {
	$this->load->view('welcome_message');
   }


   function getallmemberdata(){

        $html = $this->output->get_output();
        
        $this->load->library('pdf');
        
        $this->pdf->loadHtml($html);
        
        $this->pdf->setPaper('A4', 'landscape');
        
        $this->pdf->render();
        
        $this->pdf->stream("welcome.pdf", array("Attachment"=>0));
   }
}


Step-4: Add Route

application/config/routes.php

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


$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['resportmembers'] = "welcome/getallmemberdata";

Step 5: Add View File

application/views/resportmembers.php

<!DOCTYPE html>
<html>
<head>
	<title>Codeigniter 3 - Generate PDF from view using dompdf library with example</title>
</head>
<body>


<h2>Codeigniter 3 - Generate PDF from view using dompdf library with example - www.pakainfo.com</h2>
<table style="border:2px dashed green;width:100%;">
	<tr>
		<th style="border:2px dashed green">Id</th>
		<th style="border:2px dashed green">Member Name</th>
		<th style="border:2px dashed green">Email</th>
	</tr>
	<tr>
		<td style="border:2px dashed green">1</td>
		<td style="border:2px dashed green">Mayur</td>
		<td style="border:2px dashed green">[email protected]</td>
	</tr>
	<tr>
		<td style="border:2px dashed green">2</td>
		<td style="border:2px dashed green">Krishna</td>
		<td style="border:2px dashed green">[email protected]</td>
	</tr>
</table>


</body>
</html>

Now you can open bellow URL on your browser:

Read Also:  laravel findOrFail Eloquent Query

http://localhost:8000/resportmembers

Don’t Miss : create pdf in codeigniter using mpdf

I hope you get an idea about dompdf codeigniter.
I would like to have feedback on my infinityknow.com.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.