Search code examples
phpmysqlfpdf

FPDF dynamic table position


I am using this script in order to create a pdf with several tables.

First i get a list of employees from a table and then, for every element in that list, i want to send to the pdf file the name of the employee and a table with his info. My problem is with the Y position i think, works fine on the first page but after that the tables don't show properly. Any help would be greatly appreciated...

Here is my code:

<?php
require('fpdf.php');

$pdf=new FPDF();
$pdf->AliasNbPages();
$pdf->AddPage();

$Y = 80;
$Y_Fields_Name_position = 90;
$Y_Table_Position = 96;


$get_data = $mysqli->query("SELECT name FROM employees ORDER BY name"); 
if ($get_data) {
while ($data = $get_data->fetch_array(MYSQLI_BOTH)) {

    $result = $mysqli->query("select departamento as Departamento, categoria as Categoria, count(*) as Totales from gestiones
                                        where date(fecha) between '2015-01-01' and '2015-01-31' and employee = '$data[0]'
                                        group by categoria
                                        order by Totales desc");    
    $number_of_products = $result->num_rows;

    if ($number_of_products > 0) {

        $column_departamento = "";
        $column_categoria = "";
        $column_totales = "";
        $total = 0;

        if ($result) {
                    while ($row = $result->fetch_row()) {
                    $departamento=$row[0];
                    $categoria=utf8_decode($row[1]);
                    $totales=$row[2];

                    $column_departamento = $column_departamento.$departamento."\n";
                    $column_categoria = $column_categoria.$categoria."\n";
                    $column_totales = $column_totales.$totales."\n";

                    //Sum totales
                    $total += $row[2];

                }
            }

        $pdf->SetFont('Arial','B',12);
        $pdf->SetY($Y);
        $pdf->Cell(40,10,$data[0]);

        $pdf->SetFillColor(232,232,232);
        $pdf->SetY($Y_Fields_Name_position);
        $pdf->SetX(20);
        $pdf->Cell(30,6,'Depto.',1,0,'C',1);
        $pdf->SetX(50);
        $pdf->Cell(110,6,utf8_decode('Categoría'),1,0,'L',1);
        $pdf->SetX(160);
        $pdf->Cell(30,6,'Totales',1,0,'C',1);
        $pdf->Ln();

        $pdf->SetFont('Arial','',11);
        $pdf->SetY($Y_Table_Position);
        $pdf->SetX(20);
        $pdf->MultiCell(30,6,$column_departamento,1,'C');
        $pdf->SetY($Y_Table_Position);
        $pdf->SetX(50);
        $pdf->MultiCell(110,6,$column_categoria,1);
        $pdf->SetY($Y_Table_Position);
        $pdf->SetX(160);
        $pdf->MultiCell(30,6,$column_totales,1,'C');
        $pdf->SetX(160);
        $pdf->MultiCell(30,6,$total,1,'C');

        $Y = $Y + 90;
        $Y_Fields_Name_position = $Y_Fields_Name_position + 90;
        $Y_Table_Position = $Y_Table_Position + 90;

        $gran_total += $total;

    }

}

$start_Y= $pdf->GetY();
$pdf->SetFont('Arial','B',15);
$pdf->SetY($start_Y + 30);
$pdf->Cell(40,10,'Total de Servicios: '.$gran_total);

}

$pdf->Output();

$result->free();
$get_data->free();
$mysqli->close();
?>

Thanks in advance.


Solution

  • I added something like this and it seems to be working right:

    $start_Y = $pdf->GetY();
    
    if  ($start_Y > 200) {
    
        $pdf->AddPage();
        $Y = 10;
        $Y_Fields_Name_position = 20;
        $Y_Table_Position = 26;
    
    } else {
    
        $Y = $start_Y + 1;
        $Y_Fields_Name_position = $start_Y + 11;
        $Y_Table_Position = $start_Y + 17;
    }
    

    Thanks!