[SOLVED] CodeIgniter DOMPDF problem with thead HTML element

I need to print out some report to PDF format so the customer can print it out, save it or just review it. I used CodeIgniter for framework, then I choose DOMPDF library to fulfill my goal. But I got a little bit problem with this, when I try to print out some data on HTML table format, and the table header need to print repeatedly in every page. Here is the message that I got :
Message: Call to undefined method DOMText::getAttribute()

This problem occurred after I added a <thead> tag into my HTML report source code. Without thead tag, my report show flawlessly. Some threads out there said it’s because HTML5 parser that need to be activate in DOMPDF option. So I tried to add a one-line code to enable HTML5 parser in DOMPDF:
$dompdf = new DOMPDF();
$dompdf->setIsHtml5ParserEnabled(true);

Message: Call to undefined method DOMPDF::setIsHtml5ParserEnabled()

Message: Call to undefined method DOMPDF::setIsHtml5ParserEnabled()

But it doesn’t work for me, then I do some research in DOMPDF documentations and the codes, finally I found another way to enable HTML5 parser in DOMPDF and worked for me, the solution is very simple. Just add this line in your PDFGenerator or your PDF class that will render your PDF report:
$dompdf->set_option('enable_html5_parser', true);

Here is my complete PDFGenerator source code, this class compatible with CodeIgniter 3:

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

define('DOMPDF_ENABLE_AUTOLOAD', false);
define("DOMPDF_ENABLE_REMOTE", false);
require_once("./vendor/dompdf/dompdf/dompdf_config.inc.php");

class Pdfgenerator {
    
    public function generate($html, $filename='', $stream=TRUE, $paper = 'A4', $orientation = "portrait", $custom_size=array()) {
        $dompdf = new DOMPDF();
        $dompdf->set_option('enable_html5_parser', true);
        $dompdf->load_html($html);
        if (!empty($custom_size)) {
            $dompdf->set_paper($custom_size, $orientation);
        } else {
            $dompdf->set_paper($paper, $orientation);
        }
        $dompdf->render();
        if ($stream) {
            $dompdf->stream($filename.".pdf", array("Attachment" => 0));
        } else {
            return $dompdf->output();
        }
    }
    
}

Hope this thread will help you out guys.

Leave a Reply