可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I have a simple script that generates a PDF file. I'm getting the info I need from the database and using a foreach
to create the pages of the PDF file, and the last page I have some charts and other info that are not retrived from DB. All the pages, except this last one, must have a subheader.
My current code is:
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');
}
Basically, I need to have the subheader describing each row. But If I do this, all the lines will have the subheader:
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');
}
I need this subheader in each page, except the last one. I've tried to do some crazy code with pageNo()
function, but it didn't work.
Thank's!
回答1:
Ok guys, I solved my problem. For anyone who's looking for something similar, here is what I did: The GetY()
function return the Y position of that line, so when it's the first line of the page, this value will be 0 (or a constant, depends of your layout). I just did:
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');
}
Thank you all!
回答2:
I had this same problem trying to get a header on each page. This also connects to a MySql database. Here is what ended up working for me.
<?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();
回答3:
override the method header() of FPDF in your class.
class PDF extends FPDF
{
public function Header() {
$this->Cell(0, 8, "HEADER TEXT", 0, 0, 'R');
}
}
This method will be called automatically by the FPDF while adding a new page.