H Dindi
H Dindi

Reputation: 1552

PDF generation using DOMpdf in CodeIgniter

I am trying to generate a pdf using dompdf.

 <?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Dompdf_test extends CI_Controller {

    public function index() {
        // Load all views as normal
        //$this->load->view('phptopdfexample');
        $this->all_movements();
        // Get output html
        $html = $this->output->get_output();

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

        // Convert to PDF
        $this->dompdf->load_html($html);

        $this->dompdf->render();
        $min = 1;
        $max = 1000;
        $name = rand($min, $max);
        $this->dompdf->stream($name . '.pdf');
    }

    public function all_movements() {
        $data['stocks'] = $this->inventory->getdepartmentalmovements();
        $data['meds'] = $this->inventory->get_meds();

        $this->load->view('deptartmental_issue_pdf', $data);
    }

}

When I run the script, I get an internal server error with the following error : A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'hex'

Filename: include/style.cls.php

Line Number: 1422

How can I solve this problem?

Upvotes: 3

Views: 11034

Answers (2)

Devang Hire
Devang Hire

Reputation: 314

Step 1: Download Dompdf library from "https://github.com/dompdf/dompdf/releases".

Step 2: paste it in libraries folder like "\xamppp\htdocs\codeigiter\application\libraries".

Step 3: Also Create one file named like "Pdf.php" in libraries folder and paste below code in it.

require 'dompdf/autoload.inc.php';
use Dompdf\Dompdf;

class Pdf extends Dompdf    
{
public function __construct()
    {
    parent::__construct();      
    $dompdf = new Dompdf(); 
    }
}
?>

Step 4: Create pdf controller and paste below code in this and run controller.

  <?php defined('BASEPATH') OR exit('No direct script access allowed');

class Printbill extends CI_Controller {

public function __construct()
{
parent::__construct();
$this->load->library('pdf');
}

public function index()
{
    $this->load->library('pdf');
    $this->pdf->loadHtml('html code or variable');
    // $customPaper = array(0,0,570,570);
    //$this->pdf->set_paper($customPaper);
    $this->pdf->setPaper('A4','portrait');//landscape
    $this->pdf->render();
    $this->pdf->stream("abc.pdf", array('Attachment'=>0));
    //'Attachment'=>0 for view and 'Attachment'=>1 for download file        
}
}           
?>

Upvotes: 0

ReNiSh AR
ReNiSh AR

Reputation: 2852

This problem is fixed in dompdf 0.6

or you may correct it by adding a condition in:

dompdf/include/style.cls.php

then search for if ( is_null($col) ) (may be: Line 1422 or near of it)

if ( is_null($col) )
$col = self::$_defaults["color"];
//see __set and __get, on all assignments clear cache, not needed on direct set through __set
$this->_prop_cache["color"] = null;
$this->_props["color"] = $col["hex"];
}

add this condition also, and try.

if (is_array($col))
     $this->_props["color"] = $col["hex"];

Upvotes: 4

Related Questions