2013-03-21 1 views
1

PDF 파일을 생성하는 간단한 스크립트가 있습니다. 나는 데이터베이스에서 필요한 정보를 얻고 있으며 foreach을 사용하여 PDF 파일의 페이지를 만들고 마지막 페이지에는 DB에서 얻지 못한 일부 차트와 기타 정보가 있습니다. 마지막 페이지를 제외한 모든 페이지에는 하위 헤더가 있어야합니다.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

재정의 방법 헤더() 클래스의 FPDF. 새 페이지를 추가하는 동안

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

이 방법은 FPDF에 의해 자동으로를 호출됩니다.