2010-03-12 113 views
9

我正在使用PHP和FPDF生成帶有項目列表的PDF。我的問題是,如果項目列表進入第二或第三頁,我想一起保留項目名稱,數量和描述。現在,它將轉到第二頁,但它可能會拆分特定項目的所有細節。請幫忙!FPDF分頁問題

<?php 
require_once('auth.php');  
require_once('config.php'); 
require_once('connect.php'); 

$sqlitems="SELECT * FROM $tbl_items WHERE username = '" . $_SESSION['SESS_LOGIN'] . "'"; 
$resultitems=mysql_query($sqlitems); 

require_once('pdf/fpdf.php'); 
require_once('pdf/fpdi.php'); 


$pdf =& new FPDI(); 
$pdf->AddPage('P', 'Letter');  
$pdf->setSourceFile('pdf/files/healthform/meds.pdf'); 
$tplIdx = $pdf->importPage(1); 
$pdf->useTemplate($tplIdx); 

$pdf->SetAutoPageBreak(on, 30); 

$pdf->SetTextColor(0,0,0); 
$pdf->Ln(10); 

while($rowsitems=mysql_fetch_array($resultitems)){ 

$pdf->SetFont('Arial','B',10); 
$pdf->Cell(50,4,'Item Name:',0,0,'L'); 
$pdf->SetFont(''); 
$pdf->Cell(100,4,$rowsitems['itemname'],0,0,'L'); 

$pdf->SetFont('Arial','B',10); 
$pdf->Cell(50,4,'Quantity:',0,0,'L'); 
$pdf->SetFont(''); 
$pdf->Cell(140,4,$rowsitems['itemqty'],0,1,'L'); 

$pdf->SetFont('Arial','B'); 
$pdf->Cell(50,4,'Description:',0,0,'L'); 
$pdf->SetFont(''); 
$pdf->Cell(140,4,$rowsitems['itemdesc'],0,1,'L'); 
} 

$pdf->Output('Items.pdf', 'I'); 

?> 

回答

10

bmb是在正確的軌道上。這裏有一個更詳細的解決方案。

有很多不同的方法可以做到這一點,但是您必須根據自己的需求做出一些決定。如果你的行可能佔用了頁面的一半,那麼這可能不會對你最好,但是如果你的行通常大約2-5行,那麼這是一個好方法。

因爲我行中的第一個單元格是我的表中的多行內存單元(MultiCell in FPDF speak),所以這些行的動態高度基於此第一個單元格。所以我根據當前的Y位置,頁面高度和底部邊距,計算出行的高度將取決於字符串的寬度和單元格的寬度,然後將其與頁面上剩餘的空間進行比較:

while ($row = mysql_fetch_array($result)) { 

    // multicell content 
    $description = $row['desciption']; 

    // get column width from a column widths array 
    $column_width = $column_widths['description']; 

    $number_of_lines = ceil($pdf->GetStringWidth($description)/($column_width - 1)); 
    // I subtracted one from column width as a kind of cell padding 

    // height of resulting cell 
    $cell_height = 5 + 1; // I have my cells at a height of five so set to six for a padding 
    $height_of_cell = ceil($number_of_lines * $cell_height); 

    // set page constants 
    $page_height = 279.4; // mm (portrait letter) 
    $bottom_margin = 20; // mm 

    // mm until end of page (less bottom margin of 20mm) 
    $space_left = $page_height - $pdf.GetY(); // space left on page 
    $space_left -= $bottom_margin; // less the bottom margin 

    // test if height of cell is greater than space left 
    if ($height_of_cell >= $space_left) { 
    $pdf->Ln();       

    $pdf->AddPage(); // page break. 
    $pdf->Cell(100,5,'','B',2); // this creates a blank row for formatting reasons 
    } 

    // ... 
    // actual creation of pdf stuff 
    // ... 

} 
4

看起來你有幾個選擇。

您可以在循環中跟蹤頁面的位置,並在空間不足時發佈自己的分頁符。這要求您使用SetAutoPageBreak()來關閉自動分頁符。

另一種方法是重寫AcceptPageBreak()方法。當添加分頁符時,該方法會自動調用。如果您想將另一行壓縮到當前頁面上,您將希望返回FALSE,因此您必須跟蹤您當前正在打印的詳細信息。

-2

那麼「page-break-inside」屬性呢?我在桌子裏面試了一下,這很有幫助。

我將「page-break-inside」行內css屬性硬編碼到我的行中,並且此行在發送到打印頁面時不會中斷,分散在兩頁中。它被完全推到新頁面或留在前一頁。也許這不是一個動態的修復(從fPDF方面),但它仍然可以解決問題。

+0

這並沒有提供問題的答案。 要批評或要求作者澄清, 在他們的帖子下留下評論 - 你可以隨時評論你自己的帖子, ,一旦你有足夠的聲譽,你將能夠評論任何帖子。 – Ghost 2014-12-29 07:33:09

+0

其實它確實提供了我的情況下的解決方案。將「page-break-inside」in-line-css屬性添加到行或表中時,可以防止行/表發送到打印機頁面並在兩頁之間進行傳播。它至少是一個硬編碼的修復,如果不是fPDF方面的動態修復。 – 2015-01-13 12:06:00