Generate PDF from HTML with Dompdf in PHP & Codeignater

Dompdf Installation and Setup

Download stable release of dompdf from GitHub. Extract the downloaded dompdf archive version and place it to the directory where you want to use Dompdf.

Instantiate Dompdf Class

To use the Dompdf class, you need to include the autoloader in PHP script. Use the following PHP code to instantiate and use the dompdf class.

Basic Usage (Convert HTML to PDF)

The following example shows how to use Dompdf to convert HTML and generate PDF with minimal configuration.

  • Specify the HTML content in loadHtml() method of Dompdf class.
  • Render HTML as PDF using render() method.
  • Output the generated PDF to Browser using stream() method.

// Load HTML content 
$dompdf->loadHtml('<h2>welcome codeone </h2>'); 
 
// (Optional) Setup the paper size and orientation 
$dompdf->setPaper('A4', 'landscape'); 
 
// Render the HTML as PDF 
$dompdf->render(); 
 
// Output the generated PDF to Browser 
$dompdf->stream();

Advanced Usage

With the Dompdf library, you can easily enhance the functionality of the PDF creation. The following code generates PDF from an HTML file (pdf-content.html).

  • Get content from HTML file using file_get_contents() function in PHP.
  • Load HTML content using loadHtml() method of Dompdf class.
  • Control the PDF output using stream() function of Dompdf class.
    • $filename – (string) Name of the PDF file.
    • $options – (array) Header options.
    • Attachment – 1 = download and 0 = preview
    // Load content from html file 
    $html = file_get_contents("pdf-content.html"); 
    $dompdf->loadHtml($html); 
     
    // (Optional) Setup the paper size and orientation 
    $dompdf->setPaper('A4', 'landscape'); 
     
    // Render the HTML as PDF 
    $dompdf->render(); 
     
    // Output the generated PDF (1 = download and 0 = preview) 
    $dompdf->stream("codexworld", array("Attachment" => 0));
    

    Dompdf Useful Methods

    Dompdf library provides various methods and options to configure the PDF creation. Some of the useful methods of Dompdf class are given below that are commonly used to integrate HTML to PDF functionality.

    loadHtml(): Loads HTML content.

    • $str (string) – Required. Specify HTML to load.
    • $encoding (string) – Optional. Specify encoding.

    loadHtmlFile(): Loads content from an HTML file.

    • $file (string) – Required. Specify filename or url 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(): Sets the base path to include external stylesheets and images.

    • $basePath (string) – The 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) – Specify name of the file to be streamed (without .pdf extension).
    • $options (array) –
      • ‘compress’ => 1 or 0 – enable content stream compression.
      • ‘Attachment’ => 1 = download or 0 = preview

    Saving Output PDF File into the server

    $output = $dompdf->output();
    $fileName = $pdfname.'.pdf';
    file_put_contents('assets/Invoice-Files/'.$fileName, $output);

    Using Codeigniter

    
    
    <?php
    defined('BASEPATH') OR exit('No direct script access allowed');
    require_once 'dompdf/autoload.inc.php';
    use Dompdf\Dompdf;                                   
    class Amd_invoice extends CI_Controller
    {
    	public function __construct()                             
    	{
            parent::__construct();
    		$this->form_validation->set_message('required', 'The %s is already taken');
    		date_default_timezone_set('Asia/Kolkata');
    		$this->load->model("Admin_Model","H");
    
        } 
        public function GanaratePDF(){
               
            $dompdf = new Dompdf();
       // Load a view like you would normally do
    
    $dataMessage['userDate']=array('name'=>'Abdul Mohammad' ,'Age'=>'26');
    
    $this->load->view('amd-invoice-tem',$dataMessage);
    
    $html = $this->output->get_output();
    
    // Load library
    
    // Create pdf name based on date, hour, seconds
    $pdfname = date("FjYgias");
    $dompdf->loadHtml($html);
    $dompdf->setPaper('A4', 'landscape'); 
    $dompdf->render();
    $dompdf->stream($pdfname, array("Attachment" => 0));
     
    $output = $dompdf->output();
    $fileName = $pdfname.'.pdf';
    file_put_contents('assets/Invoice-Files/'.$fileName, $output);
    
    
        }
    }

    Output :

    Generated PDF

    Leave a Reply

    Your email address will not be published.