I hope you are very well today.
I have the following code that would be my PDF report, I am using the tcpdf library.
<?php
require_once('tcpdf/tcpdf.php');
require 'phpqrcode/qrlib.php';
$pdf_orientation = 'P';
$pdf_unit = 'px';
$pdf_pageformat = 'A4';
$pdf_unicode = true;
$pdf_encoding = 'UTF-8';
$pdf = new TCPDF($pdf_orientation, $pdf_unit, $pdf_pageformat, $pdf_unicode, $pdf_encoding, false);
$pdf->setPrintHeader(false); //para eliminar la linea superio del pdf por defecto
$pdf->setPrintFooter(false); //eliminar linea del footer
//set margins
$pdf->SetMargins(20, 20, 20);
$pdf->SetHeaderMargin(20);
$pdf->SetFooterMargin(20);
//set auto page breaks
$pdf->SetAutoPageBreak(TRUE, 20);
//set image scale factor
//$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
//set some language-dependent strings
//$pdf->setLanguageArray($l);
// ---------------------------------------------------------
// set font
$pdf->SetFont('freesans', '', 10);
$pdf->AddPage();
$html ='
<style>
.imgs{
width:150px;
height:150px;
border:1px solid red;
margin:90px;
padding:50px;
}
</style>';
include('config.php');
$queryCampostabla = ("SELECT * FROM recibir_items");
$resulCampostabla = mysqli_query($con, $queryCampostabla);
$cantidadItems = mysqli_num_rows($resulCampostabla);
$dir = 'imagenesQR/';
while ($row_expo = mysqli_fetch_array($resulCampostabla)) {
$codigoId = $row_expo['id_reci'];
$codigo = $row_expo['codigo_reci'];
$descrip_reci = $row_expo['descrip_reci'];
$ubi = $row_expo['ubi'];
$name = $codigoId.".png";
if(!file_exists($dir))
mkdir($dir);
$filename = $dir.$name;
$tamanio = 10; //Tamaño de Pixel
$level = 'H'; //Precisión Baja
$frameSize = 1; //Tamaño en blanco
QRcode::png($codigoId, $filename, $level, $tamanio, $frameSize);
$html = '';
$a = 0;
$nRegistros = $cantidadItems;
$html .= '<table style="text-align: center;" align="center">';
for ($i = 0; $i < $nRegistros; $i++) {
if ($a == 0) {
$html .= '<tr>';
}
$html .= '<td style="padding: 20px;"> <span style="font-size: 8px !important;">"'.$descrip_reci.'"</span>
<br> <span style="text-align: center !important;">"'.$codigo.'"</span>
<br> <span>"'.$ubi.'"</span>
<br> <img src="'.$filename.'" class="romi"/></td>';
if ($a == 2) {
$html .= '</tr>';
$a = 0;
} else if ($i == ($nRegistros - 1)) {
$html .= '</tr>';
} else {
$a++;
}
}
$html .= '</table>';
}
$pdf->writeHTML($html, true, false, true, false, '');
$pdf->Output('demo.pdf', 'I');
?>
The report is generated without problems and the way I need it, the only drawback is that I have a total of 4 records in my table, but it only prints the last record 4 times.
What am I doing wrong?
Any help could be very useful, thank you very much.
The problem is that inside the loop you are reinitializing the variable
$html
and it leaves you only the last record, losing even the styles. Also, the loopfor
is not necessary, because you multiply a single record by the number of results returned from the query.To have 2 cells, it is necessary to declare the variable
$a
, which you use as a counter, also outside the loop and, inside, analyze if the counter is an even number, that is, when it($a % 2)
returns zero:Important note: Do not use the PHP closing tag
?>
at the end of your scripts , because you could send spaces or line breaks that will "break" the PDF content. Similarly, the opening tag<?php
must be in the first line and first column to avoid the same problem.