Using Dompdf in CodeIgniter 3.x
Satya Kresna edited this page Sep 6, 2017
·
4 revisions
Report is usually use when we want to responsibility a things. On the web we can generate report in many ways. One of them is convert html to pdf. In PHP, also we can find out the many library / plugins / helper to do it. Several of them like FPDF, TCPDF, DOMPDF, Html2Pdf and etc.
In this case I would like to share how can I generate report with DOMPDF together with CodeIgniter 3.x. Currenty the version I'm use is CodeIgniter 3.1.4 and the DOMPDF is version 0.6.x and 0.8.0
You can see the result in production with go to this link: ci-dompdf6 and the source code in Github ci-dompdf6.
Setup
- Read my CodeIgniter-Config in Github: ci-config.
- Change the value of
$config['composer_autoload']
from$config['composer_autoload'] = FALSE;
to$config['composer_autoload'] = "./vendor/autoload.php";
- Go to composer.json in the root project and attach:
To download dompdf package. Then run
{ "require": { "dompdf/dompdf" : "0.6.*" } }
composer update
. Wait a few second and your package is ready. - Create library with the name
Pdfgenerator.php
(you can name it free) and the code contains below:<?php defined('BASEPATH') OR exit('No direct script access allowed'); define('DOMPDF_ENABLE_AUTOLOAD', false); require_once("./vendor/dompdf/dompdf/dompdf_config.inc.php"); class Pdfgenerator { public function generate($html, $filename='', $stream=TRUE, $paper = 'A4', $orientation = "portrait") { $dompdf = new DOMPDF(); $dompdf->load_html($html); $dompdf->set_paper($paper, $orientation); $dompdf->render(); if ($stream) { $dompdf->stream($filename.".pdf", array("Attachment" => 0)); } else { return $dompdf->output(); } } }
- Then in your controllers folder create
Report.php
and call your library$this->load->library('pdfgenerator')
. To generate it call$this->pdfgenerator->generate(params)
. The full code you can see in this link. - In step number 5 the
Report controller
call the viewtable_report.php
and it will generate to the report preview. The full code you can see in this link. -
IMAGE ISSUES. Since dompdf 0.6.x, I cannot call assets like image with
<?php echo base_url(); ?>
(Codeigniter helper url) for load the image to report preview. So, to do that, I do<?php echo $_SERVER['DOCUMENT_ROOT']."/yourasetsfolder/blabla.jpg or png"
and it works.
You can see the result in production with go to this link: ci-dompdf8 and the source code in Github ci-dompdf8.
Setup
- Read my CodeIgniter-Config in Github: ci-config.
- Change the value of
$config['composer_autoload']
from$config['composer_autoload'] = FALSE;
to$config['composer_autoload'] = "./vendor/autoload.php";
- Go to composer.json in the root project and attach:
To download dompdf package. Then run
{ "require": { "dompdf/dompdf" : "^0.8.0" } }
composer update
. Wait a few second and your package is ready. - Create library with the name
Pdfgenerator.php
(you can name it free) and the code contains below:<?php defined('BASEPATH') OR exit('No direct script access allowed'); require_once("./vendor/dompdf/dompdf/autoload.inc.php"); use Dompdf\Dompdf; class Pdfgenerator { public function generate($html, $filename='', $stream=TRUE, $paper = 'A4', $orientation = "portrait") { $dompdf = new DOMPDF(); $dompdf->loadHtml($html); $dompdf->setPaper($paper, $orientation); $dompdf->render(); if ($stream) { $dompdf->stream($filename.".pdf", array("Attachment" => 0)); } else { return $dompdf->output(); } } }
- Then in your controllers folder create
Report.php
and call your library$this->load->library('pdfgenerator')
. To generate it call$this->pdfgenerator->generate(params)
. The full code you can see in this link. - In step number 5 the
Report controller
call the viewtable_report.php
and it will generate to the report preview. The full code you can see in this link. -
IMAGE ISSUES. Since dompdf 0.6.x, I cannot call assets like image with
<?php echo base_url(); ?>
(Codeigniter helper url) for load the image to report preview. So, to do that, I do<?php echo $_SERVER['DOCUMENT_ROOT']."/rootproject/yourasetsfolder/blabla.jpg or png"
in localhost and it works. In server just<?php echo $_SERVER['DOCUMENT_ROOT']."/yourasetsfolder/blabla.jpg or png"
.