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.