FPDF 多单元格相同高度
FPDF Multicell same height
所以,我的问题是我正在使用 FPDF 从 php 创建一个 pdf 文件。只有一个问题。一旦文本对于单元格而言太大,它就不会换行。所以我到了尝试使用多单元的地步,但是还有另一个问题。一旦一个单元格被包裹在 table 中,我就无法让其他多单元格达到相同的高度。
这是我测试的代码。
<?php
require('../fpdf181/fpdf.php');
$pdf = new FPDF('P', 'mm', 'A4');
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Arial', '', 14);
$x = $pdf->GetX();
$y = $pdf->GetY();
$push_right = 0;
$pdf->MultiCell(50,10,"TEST shdfkjhdsafhahsjdkfkhjshakjfhdsdsfhkjdkjhsafhkjdakjhsfhkjdskjhaf", "TBRL");
$pdf->SetXY($x+50, $y);
$pdf->MultiCell(50,10,"TEST shdfkjhdsafhahsjdkfkhjshakjfhdsdsfhkjdsafsdafdsafsdafsdafddkjhsafhkjdakjhsfhkjdskjhaf", "TBRL");
$pdf->Output();
从那个代码我得到了这个:
但它应该是这样的:
这是它的工作原理,对于那些有同样问题的人:
function MultiCellRow($cells, $width, $height, $data, $pdf)
{
$x = $pdf->GetX();
$y = $pdf->GetY();
$maxheight = 0;
for ($i = 0; $i < $cells; $i++) {
$pdf->MultiCell($width, $height, $data[$i]);
if ($pdf->GetY() - $y > $maxheight) $maxheight = $pdf->GetY() - $y;
$pdf->SetXY($x + ($width * ($i + 1)), $y);
}
for ($i = 0; $i < $cells + 1; $i++) {
$pdf->Line($x + $width * $i, $y, $x + $width * $i, $y + $maxheight);
}
$pdf->Line($x, $y, $x + $width * $cells, $y);
$pdf->Line($x, $y + $maxheight, $x + $width * $cells, $y + $maxheight);
}
执行我使用的函数:MultiCellRow(3, 50, 10, ["Cell1","Cell2", "Cell3"], $pdf);
已接受的答案适用于 non-colored 背景。如果您想要有彩色背景,那么接受的答案将不会正确遮蔽 smaller 高度列。
以下代码提供与已批准答案相同的功能,但还支持彩色背景。它可能不是最干净的解决方案(因为它必须渲染 MultiCell 组件两次),但它是我可以创建的唯一实际有效的解决方案:
function MultiCellRow($pdf, $data, $width, $height,$darkenBackground){
$x = $pdf->GetX();
$y = $pdf->GetY();
$maxheight = 0;
for ($i = 0; $i < count($data); $i++) {
$pdf->MultiCell($width, $height, $data[$i],0,'C');
if ($pdf->GetY() - $y > $maxheight) $maxheight = $pdf->GetY() - $y;
$pdf->SetXY($x + ($width * ($i + 1)), $y);
}
for ($i = 0; $i < count($data); $i++) {
if($darkenBackground) $pdf->Rect($x+$width*$i,$y,$width,$maxheight,"F");
$pdf->Line($x + $width * $i, $y, $x + $width * $i, $y + $maxheight);
$pdf->SetXY($x+$i*$width,$y);
$pdf->MultiCell($width, $height, $data[$i],0,'C');
}
$pdf->Line($x + $width * count($data), $y, $x + $width * count($data), $y + $maxheight);
$pdf->Line($x, $y, $x + $width * count($data), $y);
$pdf->Line($x, $y + $maxheight, $x + $width * count($data), $y + $maxheight);
$pdf->SetY($y+$maxheight);}
输入是:
$pdf
是 pdf 对象 (new FPDF();)
$data
是行中要渲染的字符串数组
$width
是单元格宽度(整数)
$height
决定单元格 的padding/line-spacing
$darkenBackground
是布尔值。
我将代码的前半部分部分归功于“Florian7843”。我会编辑他们现有的 post,但我做了重大更改,并认为最好提供一个单独的答案。
如果有人想提供 cleaner/efficient 解决方案,请提出修改建议。
干杯!
所以,我的问题是我正在使用 FPDF 从 php 创建一个 pdf 文件。只有一个问题。一旦文本对于单元格而言太大,它就不会换行。所以我到了尝试使用多单元的地步,但是还有另一个问题。一旦一个单元格被包裹在 table 中,我就无法让其他多单元格达到相同的高度。
这是我测试的代码。
<?php
require('../fpdf181/fpdf.php');
$pdf = new FPDF('P', 'mm', 'A4');
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Arial', '', 14);
$x = $pdf->GetX();
$y = $pdf->GetY();
$push_right = 0;
$pdf->MultiCell(50,10,"TEST shdfkjhdsafhahsjdkfkhjshakjfhdsdsfhkjdkjhsafhkjdakjhsfhkjdskjhaf", "TBRL");
$pdf->SetXY($x+50, $y);
$pdf->MultiCell(50,10,"TEST shdfkjhdsafhahsjdkfkhjshakjfhdsdsfhkjdsafsdafdsafsdafsdafddkjhsafhkjdakjhsfhkjdskjhaf", "TBRL");
$pdf->Output();
从那个代码我得到了这个:
但它应该是这样的:
这是它的工作原理,对于那些有同样问题的人:
function MultiCellRow($cells, $width, $height, $data, $pdf)
{
$x = $pdf->GetX();
$y = $pdf->GetY();
$maxheight = 0;
for ($i = 0; $i < $cells; $i++) {
$pdf->MultiCell($width, $height, $data[$i]);
if ($pdf->GetY() - $y > $maxheight) $maxheight = $pdf->GetY() - $y;
$pdf->SetXY($x + ($width * ($i + 1)), $y);
}
for ($i = 0; $i < $cells + 1; $i++) {
$pdf->Line($x + $width * $i, $y, $x + $width * $i, $y + $maxheight);
}
$pdf->Line($x, $y, $x + $width * $cells, $y);
$pdf->Line($x, $y + $maxheight, $x + $width * $cells, $y + $maxheight);
}
执行我使用的函数:MultiCellRow(3, 50, 10, ["Cell1","Cell2", "Cell3"], $pdf);
已接受的答案适用于 non-colored 背景。如果您想要有彩色背景,那么接受的答案将不会正确遮蔽 smaller 高度列。
以下代码提供与已批准答案相同的功能,但还支持彩色背景。它可能不是最干净的解决方案(因为它必须渲染 MultiCell 组件两次),但它是我可以创建的唯一实际有效的解决方案:
function MultiCellRow($pdf, $data, $width, $height,$darkenBackground){
$x = $pdf->GetX();
$y = $pdf->GetY();
$maxheight = 0;
for ($i = 0; $i < count($data); $i++) {
$pdf->MultiCell($width, $height, $data[$i],0,'C');
if ($pdf->GetY() - $y > $maxheight) $maxheight = $pdf->GetY() - $y;
$pdf->SetXY($x + ($width * ($i + 1)), $y);
}
for ($i = 0; $i < count($data); $i++) {
if($darkenBackground) $pdf->Rect($x+$width*$i,$y,$width,$maxheight,"F");
$pdf->Line($x + $width * $i, $y, $x + $width * $i, $y + $maxheight);
$pdf->SetXY($x+$i*$width,$y);
$pdf->MultiCell($width, $height, $data[$i],0,'C');
}
$pdf->Line($x + $width * count($data), $y, $x + $width * count($data), $y + $maxheight);
$pdf->Line($x, $y, $x + $width * count($data), $y);
$pdf->Line($x, $y + $maxheight, $x + $width * count($data), $y + $maxheight);
$pdf->SetY($y+$maxheight);}
输入是:
$pdf
是 pdf 对象 (new FPDF();)$data
是行中要渲染的字符串数组$width
是单元格宽度(整数)$height
决定单元格 的padding/line-spacing
$darkenBackground
是布尔值。
我将代码的前半部分部分归功于“Florian7843”。我会编辑他们现有的 post,但我做了重大更改,并认为最好提供一个单独的答案。
如果有人想提供 cleaner/efficient 解决方案,请提出修改建议。
干杯!