fpdf列标题始终位于底部

时间:2014-12-23 16:30:13

标签: php mysql fpdf

我正在尝试显示来自mysql数据库的数据并使用FPDF生成pdf。数据是  相应地显示在pdf上,但列的标题始终显示在最后一条记录的下方。我无法弄清楚为什么。以下是完整的代码。

<?php if(!isset($_SESSION)) session_start();?>
<?php if(!isset($_SESSION['userID'])) header("Location: login.php"); ?>
<?php include_once 'controllers/DashboardController.php'; ?> 
<?php
define('fpdf', 'font/times');
require('fpdf/fpdf.php');

//Create new pdf file
$pdf=new FPDF();

//Open file
$pdf->Open();

//Disable automatic page break
$pdf->SetAutoPageBreak(false);

//Add first page
$pdf->AddPage();

//set initial y axis position per page
$y_axis_initial = 25;

//print column titles for the actual page
$pdf->SetFillColor(232, 232, 232);
$pdf->SetFont('Arial', 'B', 12);
$pdf->SetY($y_axis_initial);
$pdf->SetX(25);
$pdf->Cell(30, 6, 'FIRSTNAME', 1, 0, 'L', 1);
$pdf->Cell(100, 6, 'MIDDLENAME', 1, 0, 'L', 1);
$pdf->Cell(30, 6, 'SURNAME', 1, 0, 'R', 1);



//Select the Products you want to show in your PDF file
$result=$dashboardController->GETDETAILS();

//initialize counter
$i = 0;

//Set maximum rows per page
$max = 25;

//Set Row Height
$row_height = 6;
$y_axis = 0;
$y_axis = $y_axis + $row_height;

foreach($result as $data=>$row)
{
    //If the current row is the last one, create new page and print column title
    if ($i == $max)
    {
        $pdf->AddPage();

        //print column titles for the current page
        $pdf->SetY($y_axis_initial);
        $pdf->SetX(25);
        $pdf->Cell(30, 6, 'FIRSTNAME', 1, 0, 'L', 1);
        $pdf->Cell(100, 6, 'MIDDLENAME', 1, 0, 'L', 1);
        $pdf->Cell(30, 6, 'SURNAME', 1, 0, 'R', 1);

        //Go to next row
        $y_axis = $y_axis + $row_height;

        //Set $i variable to 0 (first row)
        $i = 0;
    }

    $code = $row['fname'];
    $price = $row['mname'];
    $name = $row['sname'];

    $pdf->SetY($y_axis);
    $pdf->SetX(25);
    $pdf->Cell(30, 6, $code, 1, 0, 'L', 1);
    $pdf->Cell(100, 6, $name, 1, 0, 'L', 1);
    $pdf->Cell(30, 6, $price, 1, 0, 'R', 1);

    //Go to next row
    $y_axis = $y_axis + $row_height;
    $i = $i + 1;
}


//Create file
$pdf->Output();
?>

0 个答案:

没有答案