Convert HTML to PDF using DomPDF in Codeigniter

Youtube Video Tutorial : https://www.youtube.com/watch?v=_LriTdZI0V0&t=16s



DomPDF Library Download : https://github.com/dompdf/dompdf/releases

Library/ Pdf.php

<?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';

        // instatiate and use the dompdf class
        $pdf = new DOMPDF();

        $CI = & get_instance();
        $CI->dompdf = $pdf;
    }
}

Controller/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');

  // Get ouput html
  $html = $this->output->get_output();

  // Load pdf library
  $this->load->library('pdf');

  // Load HTML content
  $this->dompdf->loadHtml($html);

  // Setup paper size and orientation
  $this->dompdf->setPaper('A4', 'landscape');

  // Render the HTML as PDF
  $this->dompdf->render();

  // Output the PDF
  $this->dompdf->stream('welcome.pdf', array('Attachment' => 0));
 }
}






Comments