Slowness with dompdf

0

I am using the library DOMPDF to generate a price list. I had a detail to use with composer that I will later consult, but installing it in a traditional way worked well for me and I was able to generate pdfs without any problem. The drawback came when trying to render information from the database.

I have a table of products with less than 50 records, I make the query, I bring it and I go around building the html for the PDF. If I make the impression of direct HTML, it is executed in its entirety in 1 second, to say the least. But if I try to pass that HTML to DOMPDF. the page lasts up to 3 minutes to execute and in some occasions pulls a white screen and does not create the PDF file. I tried testing with for and foreach cycles. the result is the same. Next I put the code of my view:

<!-- Html pure code here-->
<?php for ($i=0; $i < count($products); $i++) { ?>
    <section class="row">
        <div class="col-md-4">
            <h3><?=$products[$i]['codigo']?></h3>
        </div>
        <div class="col-md-8">
            <h3><?=$products[$i]['titulo']?></h3>
        </div>
    </section>
    <section class="row">

        <div class="col-md-4">
            <img src="<?=$products[$i]['img']?>" />
        </div>
        <div class="col-sm-8 data-producto">
            <div class="descripcion">
                <?=$products[$i]['descripcion']?>
            </div>
            <div class="precios">
                <span class="precio">

                </span>

            </div>
        </div>
    </section>
<?php } ?>

And this is the code of my controller:

$categorias = $this->post('id_categoria');
$productos = new Modelos\Producto();
// This method make the query and return an array with data.
$products = 
    $productos->get(
        ['id_categoria'=>$categorias],
        null,
        'categoria asc,titulo asc'
    );

//Helpers\Debug::imprimir($listadoProductos);
ob_start();
include 'Layout/electron/listado.tpl.php';
$html1 = ob_get_clean();
if (ob_get_length()) ob_end_clean();

$pdf = new Dompdf();
//exit($html1)
$pdf->loadHtml($html1);
$pdf->render();
$pdf->stream();

As I mentioned, I remove the creation of the PDF and the code runs fast and without problem. If I remove the for cycle and with this, the data print comes from the database, but I still do the query. The pdf is also generated quickly and without problem.

What can I do to improve this? Thanks.

    
asked by jrodriguez 26.04.2017 в 16:51
source

2 answers

1

The problem is that DOMPDF does not support bootstrap.

Problem resolution

There was no error in the structure of the html, nor in that it used buffering functions to obtain it. The problem is that I was including the bootstrap library which is not well supported by DOMPDF. In fact, I tried including only the bootstap grids system, which already executed pdf without problem but does not work correctly either . Therefore, reedit the design making a css only for the pdf and in the pdf I used tables where I used the grid structure.

Here is the link to my github ticket

    
answered by 11.05.2017 / 15:50
source
0

Try the following: if you export fast means that your variable $ html1 does not have the correct format and for some reason it causes problems to dompdf, you can do a var_dump ($ html1) and see the structure of your html; you can replicate the html as plain text and put it in a php variable and see the results.

$categorias = $this->post('id_categoria');
$productos = new Modelos\Producto();
// This method make the query and return an array with data.
$products =
    $productos->get(
        ['id_categoria'=>$categorias],
        null,
        'categoria asc,titulo asc'
    );

//Helpers\Debug::imprimir($listadoProductos);
$html1='
    <html>
    <head>
        <title>Reporte pdf</title>
        <link rel="stylesheet" href="mireporte.css">
    </head>
    <body>
        <table>
            <tr>
                <th>Nombre</th><th>Existencia</th>
            </tr>
            <tr>
                <td>Producto1</td><td>12</td>
            </tr>
            <tr>
                <td>Producto2</td><td>3</td>
            </tr>
        </table>
    </body>
    </html>
';

$pdf = new Dompdf();
//exit($html1)
$pdf->loadHtml($html1);
$pdf->render();
$pdf->stream();
    
answered by 26.04.2017 в 19:02