2013-03-21 29 views
1

我有一個生成PDF文件的簡單腳本。我從數據庫中獲取需要的信息,並使用foreach創建PDF文件的頁面,最後一頁中有一些圖表和其他信息,這些信息不是從數據庫中提取的。除最後一頁以外的所有頁面都必須有一個子標題。使用FPDF在所有頁面中添加子標題

我當前的代碼是:

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()函數做一些瘋狂的代碼,但它沒有奏效。

謝謝!

回答

0

好吧,我解決了我的問題。對於任何尋找類似的東西的人來說,這是我做的: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'); 
} 

謝謝大家!

0

我有這個相同的問題試圖讓每個頁面上的標題。這也連接到一個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(); 
0

覆蓋的方法header()函數在你的類FPDF

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

這種方法將通過FPDF,同時增加了新的一頁被稱爲自動

相關問題