Problem to generate reports with DOMPDF + Codeigniter

1

Hello,

I'm trying to generate a report using Dompdf but I'm encountering the following error

A PHP Error was encountered

Severity: Notice

Message: Undefined property: Relatorio::$dompdf

Filename: controllers/Relatorio.php

Line Number: 24

Backtrace:

File: C:\xampp\htdocs\ci_ads\application\controllers\Relatorio.php
Line: 24
Function: _error_handler

File: C:\xampp\htdocs\ci_ads\index.php
Line: 315
Function: require_once

My view:

    <!doctype html>
    <html lang="pt-br">
    <head>
    <meta charset="UTF-8">
    </head>
    <body>
    <style>
    table {
    width: 100%;
    height: 100%;
    }

    body {
    font-family: Arial, Helvetica, sans-serif;
    }

    table thead tr th td {
    border: 1px solid black;
    }

    table thead tr th {
    background-color: #bac6cb;
    font-size: 17px;
    }

    table tbody tr td {
    background-color: #9ba7ac;
    font-size: 14px;
    }
    </style>

    <div id="flex-box">
    <table>
    <thead>
    <tr>
    <td style="text-align: center" colspan="2"><h2>Alunos com maior número de locação</h2></td>
    </tr>
    <tr style="text-align: center">
    <th>Nome do aluno</th>
    <th>Quantidade de locação</th>
    </tr>
    </thead>
    <tbody>
    <?php foreach ($rel as $linha) {
    echo '<tr>';
    echo "<td style=\"text-align: center\">" . $linha['nome'] . "</td>";
    echo "<td style=\"text-align: center\">" . $linha['count'] . "</td>";
    echo '</tr>';
    } ?>
    </tbody>
    </table>
    <p>
    Relatório de alunos que mais locaram E-Book &reg;
   </p>
    </div>
    </body>
    </html>

My Controller

class Report extends CI_Controller {

function __construct() {
    parent::__construct();
    $this->load->model('relatorio_model');
}

public function index() {
    $this->load->view('template/header');
    $this->load->view('relatorio/index');
    $this->load->view('template/footer');
}



public function geraRelatorio() {
    $dados = $this->input->post();
    if ($dados['tipo_relatorio'] == 'REL_LIVRO_MAIS_LOCADO') {
         $data['rel'] = $this->relatorio_model->queryAlunosMaisLocaram();
        $this->load->library('pdf');
        $html = $this->load->view('relatorio/livros_mais_locados', $dados, true);
        $this->dompdf->load_html($html);
        $this->dompdf->set_paper("A4", "portrait"); // to change page orientation to landscape, change the parameter “portrait” to “landscape”
        $this->dompdf->render();
        $filename = "mypdf.pdf";
        $this->dompdf->stream($filename); // to Download PDF
    }
    if ($dados['tipo_relatorio'] == 'REL_ALUNOS_MAIS_LOCARAM') {
        $data['rel'] = $this->relatorio_model->queryAlunosMaisLocaram();
        $this->load->library('pdf');
        $html = $this->load->view('relatorio/alunos_mais_locaram', $dados, true);
        $this->dompdf->load_html($html);
        $this->dompdf->set_paper("A4", "portrait"); // to change page orientation to landscape, change the parameter “portrait” to “landscape”
        $this->dompdf->render();
        $filename = "mypdf.pdf";
        $this->dompdf->stream($filename); // to Download PDF
    }
}
}

Would anyone have any suggestions? Thanks

    
asked by anonymous 01.12.2016 / 16:34

1 answer

1

To begin with, I would recommend a more versatile and less complicated library, such as mPDF . But if you really want to continue using this one, at least look for the most current version: dompdf .

This error means that the Relatorio class does not have the $dompdf property that you are invoking. This is because of the way you are loading the library into CodeIgniter . Before using an external library on CodeIgniter read here carefully.

Aware of these specifications, one of the right ways to do this would be to declare dompdf as a library in /application/libraries/PdfCreator.php :

<?php

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

class PdfCreator {

    public function __construct() {
        require_once('/usr/share/php/dompdf/vendor/autoload.php');
        $this->dompdf = new Dompdf\Dompdf();
    }

}
$ci = & get_instance();
$ci->PdfCreator = new PdfCreator();

Declared library, loads into instance (I prefer autoload /application/config/autoload.php ). In case, you will load it in libraries :

$autoload['libraries'] = array(
    'PdfCreator'
);

Another thing you need to understand is that dompdf will not render $html the way you are doing because the $this->load->view() method returns object , not string . But you can correct this by using the correct property of the returned object, which is final_output . At controller , the method that creates pdf looks like this:

function geraRelatorio(){
    $dompdf = $this->PdfCreator->dompdf;
    $dompdf->set_option('isHtml5ParserEnabled', true);
    $data['rel'] = [0=>['nome'=>'teste','count'=>'2']];
    $view = $this->load->view('pages/dompdf',$data);
    $html = $view->output->final_output;
    $dompdf->loadHtml($html);
    $dompdf->setPaper('A4', 'portrait');
    $dompdf->render();
    $dompdf->stream();
}

More: you are creating $data['rel'] but you are only passing $dados to view . However, its view makes a foreach in array $rel . Beware of this, or you will have another problem at the time of generating the document, because the program will look for a $rel variable that does not exist in the scope of it.

Plus: dompdf has serious problems rendering html malformed, and the developer itself that's what you say . Therefore, any error at the time of declaring elements will generate "unexpected" crashes. I say this because in your view you are declaring the <style> in the body of the html document, and this will give you a headache when using that library. I tried to avoid this with $dompdf->set_option('isHtml5ParserEnabled', true); , but it seems that was not enough.

I made a view as soon as it worked:

<!DOCTYPE html>
<html>
    <head>
        <title>Teste</title>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link href="../assets/dompdf.css" rel="stylesheet" type="text/css">
    </head>
    <body>
        <div id="flex-box">
            <table>
                <thead>
                    <tr>
                        <td style="text-align: center" colspan="2"><h2>Alunos com maior número de locação</h2></td>
                    </tr>
                    <tr style="text-align: center">
                        <th>Nome do aluno</th>
                        <th>Quantidade de locação</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    foreach ($rel as $linha) {
                        echo '<tr>';
                        echo "<td style=\"text-align: center\">" . $linha['nome'] . "</td>";
                        echo "<td style=\"text-align: center\">" . $linha['count'] . "</td>";
                        echo '</tr>';
                    }
                    ?>
                </tbody>
            </table>
            <p>
                Relatório de alunos que mais locaram E-Book &reg;
            </p>
        </div>
    </body>
</html>

That's it.

    
04.12.2016 / 23:23