在所有FPDF页面中添加子标题


Add sub-headers in all pages with FPDF

我有一个生成PDF文件的简单脚本。我正在从数据库中获取我需要的信息,并使用foreach来创建PDF文件的页面,最后一页我有一些图表和其他未从DB检索的信息。所有的页面,除了最后一页,都必须有副标题。

我当前的代码是:

foreach ($sql as $key => $value) 
{
    $pdf->Cell(20, $line_height, $value['sale_id'], '', '', 'L');
    $pdf->Cell(90, $line_height, $value['product'], '', '', 'L');
    $pdf->Cell(90, $line_height, $value['ammount'], '', '', 'L');
    $pdf->Cell(90, $line_height, $value['value'], '', '', 'L');
}
基本上,我需要有描述每一行的子标题。但是如果我这样做,所有的行都会有子标题:
foreach ($sql as $key => $value) 
{
    $pdf->SetFont('Arial', 'B', $font_size);
    $pdf->Ln(2 * $line_height);
    $pdf->Cell(20, $line_height, 'Number', '', '', 'L');
    $pdf->Cell(120, $line_height, 'Product', '', '', 'L');
    $pdf->Cell(40, $line_height, 'Ammount', '', '', 'L');
    $pdf->Cell(40, $line_height, 'Value ($)', '', '', 'L');
    // 
    $pdf->SetFont('Arial', 'B', $font_size);
    $pdf->Ln(2 * $line_height);
    $pdf->Cell(20, $line_height, $value['sale_id'], '', '', 'L');
    $pdf->Cell(120, $line_height, $value['product'], '', '', 'L');
    $pdf->Cell(40, $line_height, $value['ammount'], '', '', 'L');
    $pdf->Cell(40, $line_height, $value['value'], '', '', 'L');
}

除了最后一页,我在每一页都需要这个子标题。我试着用pageNo()函数做一些疯狂的代码,但它没有工作。

谢谢的!

好了,我的问题解决了。对于任何正在寻找类似东西的人,以下是我所做的:GetY()函数返回该行的Y位置,因此当它是页面的第一行时,此值将为0(或常数,取决于您的布局)。我刚刚做了:

foreach ($sql as $key => $value) 
{
    if ($pdf->GetY() == 0)
    {
        $pdf->SetFont('Arial', 'B', $font_size);
        $pdf->Ln(2 * $line_height);
        $pdf->Cell(20, $line_height, 'Number', '', '', 'L');
        $pdf->Cell(120, $line_height, 'Product', '', '', 'L');
        $pdf->Cell(40, $line_height, 'Ammount', '', '', 'L');
        $pdf->Cell(40, $line_height, 'Value ($)', '', '', 'L');
    }
    $pdf->SetFont('Arial', 'B', $font_size);
    $pdf->Ln(2 * $line_height);
    $pdf->Cell(20, $line_height, $value['sale_id'], '', '', 'L');
    $pdf->Cell(120, $line_height, $value['product'], '', '', 'L');
    $pdf->Cell(40, $line_height, $value['ammount'], '', '', 'L');
    $pdf->Cell(40, $line_height, $value['value'], '', '', 'L');
}

谢谢大家!

在你的类中重写FPDFheader()方法。

class PDF extends FPDF
{
    public function Header() {
        $this->Cell(0, 8, "HEADER TEXT", 0, 0, 'R');
    }
}

在添加新页面时,FPDF将自动调用

我在试图为每个页面添加标题时遇到了同样的问题。这也连接到MySql数据库。以下是我最终成功的方法。

<?php
//PDF USING MULTIPLE PAGES
//FILE Originally CREATED BY: Carlos José Vásquez Sáez
//I fixed this on May 3/2013 as it was not working correctly for me.
define('FPDF_FONTPATH', '/font/');
require('fpdf.php');
//Connect to your database
$link = mysql_connect("localhost","","") or die ('Could not select database.');
$db_select = mysql_select_db("", $link) or die ('Could not select database.');

//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 = 10;
//Set Row Height
$row_height = 8;
//print column titles for the actual page
$pdf->SetFillColor(232, 232, 232);
$pdf->SetFont('Arial', 'B', 11);
$pdf->SetY($y_axis_initial);
$pdf->SetX(10);
$pdf->Cell(80, $row_height, 'Products', 1, 0, 'C', 1);
$pdf->Cell(20, $row_height, 'Price', 1, 0, 'C', 1);
$pdf->Cell(30, $row_height, 'Customer', 1, 0, 'C', 1);
//Select the Products you want to show in your PDF file
$result=mysql_query('SELECT Product,Price,Customer FROM your_table ORDER BY Product', $link);
//initialize counter
$i = 0;
//Set maximum rows per page
$max = 30;
//Data Table y axis position starting point
$y_axis = $y_axis_initial + $row_height;
while($row = mysql_fetch_array($result))
{
    //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(10);
        $pdf->Cell(80, $row_height, 'Product', 1, 0, 'C', 1);
        $pdf->Cell(20, $row_height, 'Price', 1, 0, 'C', 1);
        $pdf->Cell(30, $row_height, 'Customer', 1, 0, 'C', 1);
        //Set $i variable to 0 (first row)
        $i = 0;
    //Reset the y axis value
    $y_axis = $y_axis_initial + $row_height;;
    }
    $Product = $row['Product'];
    $Price = $row['Price'];
    $Cust = $row['Customer'];
    $pdf->SetY($y_axis);
    $pdf->SetX(10);
    $pdf->Cell(80, $row_height, $Product, 1, 0, 'L', 1);
    $pdf->Cell(20, $row_height, $Price, 1, 0, 'L', 1);
    $pdf->Cell(30, $row_height, $Cust, 1, 0, 'L', 1);
    //Go to next row
    $y_axis = $y_axis + $row_height;
    $i = $i + 1;
}
mysql_close($link);
//Create file
$pdf->Output();