@Zedd Answer is right
but for the latest version as on this post date, the following will work.
application/libraries/Pdf.php
<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
* CodeIgniter PDF Library
*
* Generate PDF's in your CodeIgniter applications.
*
* @package CodeIgniter
* @subpackage Libraries
* @category Libraries
* @author Chris Harvey
* @license MIT License
* @link https://github.com/chrisnharvey/CodeIgniter- PDF-Generator-Library
*/
require_once APPPATH.'third_party/dompdf/autoload.inc.php';
use DompdfDompdf;
class Pdf extends DOMPDF
{
/**
* Get an instance of CodeIgniter
*
* @access protected
* @return void
*/
protected function ci()
{
return get_instance();
}
/**
* Load a CodeIgniter view into domPDF
*
* @access public
* @param string $view The view to load
* @param array $data The view data
* @return void
*/
public function load_view($view, $data = array())
{
$dompdf = new Dompdf();
$html = $this->ci()->load->view($view, $data, TRUE);
$dompdf->loadHtml($html);
// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'landscape');
// Render the HTML as PDF
$dompdf->render();
$time = time();
// Output the generated PDF to Browser
$dompdf->stream("welcome-". $time);
}
}
At your controller
function pdf_gen(){
$this->load->library('pdf');
$this->pdf->load_view('main_report');
}
Obviously main_report is the view file at application/views/main_report
and you can send data for the view file like the following too
$this->pdf->load_view('main_report', $data);
And Note
I placed the dompdf folder in the application/thirdparty folder and not in the application/libraries.
Make sure you have commented out all print, echo functions in your controller method, else you might get an error saying something similar to "the pdf file is damaged".
Hope this post was usefull.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…