FPDF分页问题

发布于 2024-08-25 00:56:52 字数 1252 浏览 13 评论 0原文

我正在使用 PHP 和 FPDF 生成包含项目列表的 PDF。我的问题是,如果项目列表进入第二页或第三页,我想将项目名称、数量和描述保留在一起。现在,它将转到第二页,但它可能会拆分特定项目的所有详细信息。请帮忙!

<?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');

?>

I am using PHP and FPDF to generate a PDF with a list of items. My problem is if the item list goes on to a second or third page, I want to keep the Item Name, Quantity and Description together. Right now, it will go to a second page, but it may split up all of the details for a particular item. PLEASE HELP!

<?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');

?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

很酷不放纵 2024-09-01 00:56:52

bmb走在正确的轨道上。这是一个更详细的解决方案。

有多种不同的方法可以做到这一点,但您必须根据自己的需要做出一些决定。如果您的行可能占据页面的一半,那么这可能不会最适合您,但如果您的行通常约为 2 - 5 行长,那么这是一个很好的方法。

因为行中的第一个单元格是表格中的多行单元格(FPDF 中的 MultiCell),所以行的高度基于第一个单元格。因此,我根据字符串宽度和单元格的宽度计算出行的高度,然后根据当前的 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
  // ...

}

bmb is on the right track. Here is a little more detailed solution.

There are a number of different ways to do this, but you'll have to make some decisions based on what you want. If you have rows that may take up half the page, then this probably isn't going to work the best for you, but if your rows are typically about 2 - 5 lines long then this is a good method.

Because my first cell in the row is a multiple lined cell (MultiCell in FPDF speak) in my table, the rows are of dynamic height based on this first cell. So I figure out how high the row is going to be based on the string width and the width of the cell and then compare that with the room left on the page, based on the current Y position, the page height and the bottom margin:

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
  // ...

}
已下线请稍等 2024-09-01 00:56:52

看来你有几个选择。

您可以在循环时跟踪您在页面上的位置,并在空间不足时发出自己的分页符。这需要您使用 SetAutoPageBreak() 来关闭自动分页符。

另一种方法是重写 AcceptPageBreak() 方法。添加分页符时会自动调用该方法。如果您想将另一行挤到当前页面上,则需要返回 FALSE,因此您必须跟踪当前正在打印的详细信息。

It seems like you have a few options.

You can keep track of where you are on the page as you go through your loop, and issue your own page break when you run out of space. This requires that you use SetAutoPageBreak() to turn off auto page breaks.

Another method is to override the AcceptPageBreak() method. That method is called automatically when a page break will be added. You would want to return FALSE if you want to squeeze another line onto the current page, so you would have to keep track of which detail you're currently printing.

情深如许 2024-09-01 00:56:52

“page-break-inside”属性怎么样?我在桌子内尝试过,它很有帮助。

我将“page-break-inside”内联 css 属性硬编码到我的行中,并且该行在发送到打印页面时不再中断,分布在两个页面上。它要么完全推到新页面,要么留在上一页。也许这不是一个动态修复(从 fPDF 方面来看),但它仍然解决了问题。

What about the "page-break-inside" property? I tried it inside a table and it helps.

I hardcoded "page-break-inside" in-line css property to my row and this row didn't break anymore when it was sent to printing pages, spreading across two pages. It was pushed either completely to the new page or left on the previous. Maybe it is not a dynamic fix (from the fPDF side of things), but it still resolves the issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文