pdf - FPDF分页问题

标签 pdf while-loop fpdf page-break

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

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

?>

最佳答案

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

}

关于pdf - FPDF分页问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2430001/

相关文章:

python - 如何检测 pdf 文本是否已使用 PyPDF2.extractText 成功提取?

jquery - 在嵌入标签中使用响应式显示 pdf

php - fpdf 单元格未正确显示连接的 php 字符串

c# - 使用 iTextSharp 库提取 pdf 文件中包含的签名图像

Java 条件循环

java - Zip4j 库 ZipFile.setPassword() 方法在非空输入上抛出 NullPointerException

Linux/Bash - 为 "find"创建循环以输出到文件

php - FPDF 和电视/电影剧本格式

使用 FPDF 的 PHP 转 PDF 无法打开我的 pdf 文件(损坏)

c# - iTextSharp 删除线