FPDF动态表位置

时间:2015-07-30 15:33:43

标签: php mysql fpdf

我正在使用此script来创建包含多个表的pdf。

首先,我从表中获取员工列表,然后,对于该列表中的每个元素,我想向pdf文件发送员工的姓名和包含其信息的表。 我的问题是Y位置,我认为,在第一页上工作正常,但之后表格没有正确显示。任何帮助将不胜感激......

这是我的代码:

<?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();
?>

提前致谢。

1 个答案:

答案 0 :(得分:0)

我添加了类似的东西,它似乎正常工作:

$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;
}

谢谢!