2010-03-12 63 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