Blog for Learning

| lesson material | material summary | questions and answers | definitions | types and examples | other information | materi pelajaran | ringkasan materi | pertanyaan dan jawaban | definisi | jenis-jenis dan contoh-contoh | informasi lainnya |

Powered by Blogger.


How to use Dompdf on Codeigniter

Dompdf is a PHP library for creating PDFs.
Dompdf creates PDFs from HTML, making it more practical. Besides that, we can use CSS in it.
Currently Dompdf supports CSS version 2.1.
To use Dompdf on Codeigniter , please follow the steps below.

1. Activate the Autoload Composer

We will install Dompdf through the Composer .
Therefore, autoload for the Composer needs to be activated.
Please open the file application/config/config.php, then fill in the Composer autoload section like this.
$config['composer_autoload'] = "vendor/autoload.php";

2. Install Dompdf

Next we install dompdfwith the command:
composer require dompdf/dompdf
Run the command in the project directory.
Install Dompdf on Codeigniter

3. Make Your Own Library

In order not to rewrite the same code in every use of Dompdf, we just create our own library.
Please create a new file in the directory application/librarieswith the name pdf.php.
Contents like the following.
<?php defined('BASEPATH') OR exit('No direct script access allowed');
 * CodeIgniter DomPDF Library
 * Generate PDF's from HTML in CodeIgniter
 * @packge        CodeIgniter
 * @subpackage        Libraries
 * @category        Libraries
 * @author        Ardianta Pargo
 * @license        MIT License
 * @link
use Dompdf\Dompdf;
class Pdf extends Dompdf{
     * PDF filename
     * @var String
    public $filename;
    public function __construct(){
        $this->filename = "laporan.pdf";
     * 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()){
        $html = $this->ci()->load->view($view, $data, TRUE);
        // Render the PDF
            // Output the generated PDF to Browser
               $this->stream($this->filename, array("Attachment" => false));
The code above is the exstends class from the Dompdf class .

4. Making a Controller

Create a function inside the Controller to use the library that we just created.
public function laporan_pdf(){

    $data = array(
        "dataku" => array(
            "nama" => "Petani Kode",
            "url" => ""


    $this->pdf->setPaper('A4', 'potrait');
    $this->pdf->filename = "laporan-petanikode.pdf";
    $this->pdf->load_view('laporan_pdf', $data);

The loaded view file is named laporan_pdf, meaning we have to create a view file named laporan_pdf.phpin application/views.

5. Make a View

File: application/views/laporan_pdf.php
<pre><?php print_r($dataku); ?></pre>

6. Experiment

PDF reports on CodeIgniter use DomPDF
That's how to use Dompdf on Codeigniter.
The rest you can experiment alone.
If it's still not clear, you can check this codeigniter-dompdfrepository .

0 Komentar untuk "How to use Dompdf on Codeigniter"

Silahkan berkomentar sesuai artikel

Template By Kunci Dunia
Back To Top