php - 如何在 php 中使用 FPDF 在多列上换行?
问题描述
我在使用 Php 的 FPDF 中有一个问题。
我现在在我的代码中拥有的是第 1 列将始终遵循第2 列的高度,如下所示基于字符长度。
问题是只有第 2 列有效。
当其中一列增加其字符长度时,如何使/允许任何最小列高始终遵循最大单元格高度。
我当前的代码如下。
索引.php
class myPDF extends FPDF
{
function HeaderTable(){
$this->SetFont('Arial','B',10);
$this->Cell(275, 5, 'TABLE DATA',0,0);
$this->Ln();
$this->Cell(275, 1, '',0,0);
$this->Ln();
$this->SetFont('Arial','B',6);
$this->Cell(35,5,'COLUMN 1',1,0);
$this->Cell(120,5,'COLUMN 2',1,0);
$this->Cell(120,5,'COLUMN 3',1,0);
$this->Ln();
}
function ViewTable(){
$datatable = array(
array(
"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum ",
"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum ",
"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum "
),
array(
"18 Sept 2018",
"This is second row of data",
"Data here for second row",
),
array(
"18 Sept 2018",
"This is third row of data",
"Data here for third row",
),
);
foreach($datatable as $item)
{
$cellWidth=120;
$cellHeight=5;
if( ($this->GetStringWidth($item[1]) && $this->GetStringWidth($item[2])) < $cellWidth)
{
$line = 1;
$line2 = 1;
}
else
{
$textLength=strlen($item[1]);
$errMargin=10;
$startChar=0;
$maxChar=0;
$textArray=array();
$tmpString="";
$textLength2=strlen($item[2]);
$errMargin2=10;
$startChar2=0;
$maxChar2=0;
$textArray2=array();
$tmpString2="";
// 1
while($startChar < $textLength)
{
while($this->GetStringWidth($tmpString) < ($cellWidth-$errMargin) &&
($startChar+$maxChar) < $textLength)
{
$maxChar++;
$tmpString=substr($item[1], $startChar, $maxChar);
}
$startChar=$startChar+$maxChar;
array_push($textArray, $tmpString);
$maxChar=0;
$tmpString='';
}
// 1
// 2
while($startChar2 < $textLength2)
{
while($this->GetStringWidth($tmpString2) < ($cellWidth-$errMargin2) &&
($startChar2+$maxChar2) < $textLength2)
{
$maxChar2++;
$tmpString2=substr($item[2], $startChar2, $maxChar2);
}
$startChar2=$startChar2+$maxChar2;
array_push($textArray2, $tmpString2);
$maxChar2=0;
$tmpString2='';
}
// 2
$line=count($textArray);
$line2=count($textArray2);
}
$this->Cell(35,($line * $cellHeight),$item[0],1,0);
$xPos=$this->GetX();
$yPos=$this->GetY();
$this->MultiCell($cellWidth, $cellHeight, $item[1],1);
$this->SetXY($xPos + $cellWidth, $yPos);
$this->MultiCell($cellWidth, $cellHeight, $item[2],1);
$this->SetXY($xPos + $cellWidth, $yPos);
}
}
}
$pdf = new myPDF('L','mm','A4');
$pdf->AddPage();
$pdf->HeaderTable();
$pdf->ViewTable();
$pdf->Output();
感谢有人可以帮助我解决这个问题。
谢谢。
解决方案
要使用表格和多单元格,您应该使用 PDF_MC_Table 类扩展 fpdf 类。 在此页面上,您将找到可以帮助您的源代码和示例。创建这个类是为了处理表格,我希望这对你有帮助。
推荐阅读
- node.js - 如何解决 Firebase 的谷歌云功能的截止日期超出错误?
- breeze - 将承载令牌添加到 Breezejs fetchMetadata 调用
- keras - 无法在 keras 中获得预期的目标形状
- android - 如何在修改之前在 AsyncTask 中使用变量?
- css - React 和 CSS 转换,我错过了什么吗?
- c# - 对 ? 的行为感到困惑。操作员
- javascript - 在 JavaScript / TypeScript 中检测字符串中的模式
- r - 编写一个函数,其输入是函数中调用的数据框的一列
- amazon-web-services - API 版本控制解决方案
- google-bigquery - 使用 UDF 和 javascript 时出现错误消息 - 项目 ____ 尚未启用 BigQuery