2012-09-10 72 views
6

我有這個循環打印6行(多單元)約30次。問題是,當它到達底部頁面時,它從多單元打印2或3行,並在下一頁打印其他3行。如果當前頁面上的所有6行沒有足夠的空間,我想讓它在下一頁上打印所有6行。我使用此代碼:fpdf分頁問題

$height_of_cell = 60; mm 
$page_height = 279.4; // mm (portrait letter) 
$bottom_margin = 20; // mm 
$space_left = $page_height - $p->GetY(); // space left on page 
$space_left -= $bottom_margin; // less the bottom margin 
if ($height_of_cell >= $space_left) { 
$p->Ln();       
$p->AddPage(); // page break 
$p->Cell(100,5,'','B',2); // this creates a blank row for formatting reasons 
} 

但它不起作用。任何解決方案謝謝!

回答

19

使用GetY獲取當前位置,從文檔高度中減去它。如果小於6倍(有6行)多單元高度,則使用AddPage強制分頁。

我知道你解決了這個問題,但爲了其他人的利益,這應該給出一個廣泛的想法。

<?php 
$p = new FPDF(); 
$p->AddPage(); 
$p->SetFont('Arial','B',16); 
$p->SetAutoPageBreak(false); 
$height_of_cell = 60; // mm 
$page_height = 286.93; // mm (portrait letter) 
$bottom_margin = 0; // mm 
    for($i=0;$i<=100;$i++) : 
    $block=floor($i/6); 
    $space_left=$page_height-($p->GetY()+$bottom_margin); // space left on page 
     if ($i/6==floor($i/6) && $height_of_cell > $space_left) { 
     $p->AddPage(); // page break 
     } 
    $p->Cell(100,10,'This is a text line - Group '.$block,'B',2); 
    endfor; 
$p->Output(); 
?> 
+0

嗨,我修改了這樣的代碼,但它什麼也沒做。你在某處看到錯誤嗎? –

+0

我想通了。它正在工作。 –

+0

GReat - 只是編輯我的答案以進一步提供幫助:) – Mark