PDF is the most used format to create the document in the web application. PDF file provides a simple and user-friendly way to download the bunch of data in a file. Before download the web page content, the content needs to be converted from HTML to PDF. The HTML to PDF conversion can be easily done using PHP library.
Dompdf is a PHP library that helps to generate PDF from HTML content. It’s very easy to convert HTML to PDF in PHP with Dompdf. If your application built with CodeIgniter, a PDF library needs to be created to generate PDF using Dompdf. In this tutorial, we will show you how to convert HTML to pdf and generate PDF using Dompdf in CodeIgniter.
The index()
function of Welcome controller generates PDF from HTML.
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index(){
$this->load->view('welcome_message');
// Get output html
$html = $this->output->get_output();
// Load pdf library
$this->load->library('pdf');
// Load HTML content
$this->dompdf->loadHtml($html);
// (Optional) Setup the paper size and orientation
$this->dompdf->setPaper('A4', 'landscape');
// Render the HTML as PDF
$this->dompdf->render();
// Output the generated PDF (1 = download and 0 = preview)
$this->dompdf->stream("welcome.pdf", array("Attachment"=>0));
}
}
CodeIgniter PDF library (Pdf.php):
The CodeIgniter PDF library is a custom library that helps to convert HTML output to PDF using DOMPDF.
<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
* CodeIgniter PDF Library
*
* Generate PDF in CodeIgniter applications.
*
* @package CodeIgniter
* @subpackage Libraries
* @category Libraries
* @author CodexWorld
* @license https://www.codexworld.com/license/
* @link https://www.codexworld.com
*/
// reference the Dompdf namespace
use Dompdf\Dompdf;
class Pdf
{
public function __construct(){
// include autoloader
require_once dirname(__FILE__).'/dompdf/autoload.inc.php';
// instantiate and use the dompdf class
$pdf = new DOMPDF();
$CI =& get_instance();
$CI->dompdf = $pdf;
}
}
?>
Dompdf Library (/dompdf):
The custom PDF library uses Dompdf to generate PDF. So, include the Dompdf library to instantiate the Dompdf class.
The following are some useful methods of Dompdf library to implement HTML to PDF conversion functionality.
Dompdf library has various options to configure the PDF generation. In the example code, some most used configuration options (paper size and orientation, PDF output, etc.) are used. You can see all other available options from here.
Our custom Dompdf library provides the easiest way to convert HTML to PDF in CodeIgniter using Dompdf. Our example code shows the most useful configuration options to generate PDF in CodeIgniter. All the available functionality of Dompdf class can be used in CodeIgniter PDF library. The example code generates the PDF from HTML of the view. You can convert any dynamic HTML and generate PDF as per your needs.
Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request
I want to use PDF of version PDF/A-3 and want to add xml data to this pdf, How can I do this using Dompdf?
how to save word to pdf?
Nice
how to save the pdf to a specific folder??? please help
its very helpful tutorial thank you for sharing.