PDF is the most used format to create the document in the web application. PDF file gives an easy and convenient way to download the bunch of data in a file. Before download the web page data or content, the information needs to be change from HTML to PDF. The HTML to PDF change can be easily done using PHP library.
DOMPDF is a PHP library that helps to create PDF from HTML content. It’s very easy to transform HTML to PDF in PHP with DOMPDF. If we create our application with CodeIgniter, then we need a PDF library needs to be created to generate PDF using Dompdf. In this blog, we will show you how can we convert HTML to pdf and generate PDF using Dompdf in CodeIgniter.Controller :
The index() function of Pdfcreate controller generates PDF from HTML.- Get output HTML from pdfcreate view.
- Loading the CodeIgniter Pdf library to use Dompdf classes.
- Transform HTML content and generate PDF using Dompdf.
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Pdfcreate extends CI_Controller {
public function index(){
$this->load->view('pdfcreate');
// 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("test.pdf", array("Attachment"=>0));
}
}Library :
CodeIgniter PDF library (Pdf.php):
The CodeIgniter PDF library is a custom library that helps to change HTML output data to PDF using DOMPDF class.
<?php defined('BASEPATH') OR exit('No direct script access allowed');
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 File uses Dompdf to generate PDF. So, we need to include the Dompdf library to instantiate the Dompdf class. Useful Methods of Dompdf
There are some useful methods of Dompdf library to implement HTML to PDF conversion functionality.
$encoding (string) – Optional. Specify encoding.
- loadHtml(): Loads HTML content.
$encoding (string) – Optional. Specify encoding.
- loadHtmlFile(): Loads content from an HTML file. $file (string) – Required. Specify file path to load.
- output(): Returns the PDF as a string. $options (array) – Optional. Specify whether content stream compression will enable. (compress => 1 or 0)
- render(): Renders the HTML to PDF.
- setBasePath(): This function is used to sets the base path to include external stylesheets and images. $basePath (string) – This function base path to be used when loading the external resources URLs.
- setPaper(): Sets the paper size & orientation. $size (string|array) – ‘letter’, ‘legal’, ‘A4’, etc. $orientation (string) – ‘portrait’ or ‘landscape’.
- stream(): Streams the PDF to the client. $filename (string) – This is specifies name of the file (without .pdf extension). $options (array) – ‘compress’ => 1 or 0 – enable content compression. ‘Attachment’ => 1 = For download or 0 = For preview
0 Comments