[英]TCPDF. Is it possible to make the cell fillable after generating using TCPDF?
[英]Is it possible to make a TCPDF element with maximum height?
我正在使用PHP使用TCPDF。 我試圖讓那一定是只有一頁長的PDF文檔。 該文檔包括一個表,該表的數據具有動態確定的行數。
我想做的就是給這張桌子一個最大的高度(例如10厘米),如果桌子的字體超過這個限制,要縮小它的字體大小。 這樣,數據仍然存在,但是文檔將適合一頁。 這可能嗎? 我嘗試使用WriteHTML()
方法,但似乎忽略了我給它的高度。 (也就是說,如果數據結束,它只會繼續寫入而不是切斷數據。)
這可能嗎?
一種方法是設置單個MultiCell
或Cell
輸出的大小。 涉及很多視覺調整,但是您可以非常精確地進行調整。 為了簡單起見,我創建了一個具有固定行高的粗略示例。 基本上,這個想法是在$ln
參數設置為0的情況下,將最大高度和位置(Multi)Cells
設置在上一個(Multi)Cells
的右側。(或在每次調用之前設置坐標。)您可以在此處看到示例輸出文件: https : //s3.amazonaws.com/rrbits/maxheight.pdf
MultiCell
具有autofit
設置,該設置將為您處理字體的縮小,我在MultiCell
示例中使用了該MultiCell
。 一個潛在的警告是它將允許包裝。
Cell
沒有autofit參數,但是您可以通過將字體大小設置為合理的最大值來模擬它,使用GetStringWidth
檢查單元格字符串的寬度並減小字體大小直到適合為止。 [編輯:我這里不做,但是在輸出單元格之后恢復字體大小是個好主意,否則您可能會得到一些意想不到的結果。](請參見第105行的循環)。 ( Cell
確實有一些字體擴展選項可用,請參閱TCPDF示例004 ,但它們並沒有完全按照您的要求進行。)
<?php
require_once('TCPDF-6.2.17/tcpdf.php');
// create new PDF document
$pdf = new TCPDF('L', 'mm', array(130,130), true, 'UTF-8', false);
$pdf->SetFont('times', '', 8);
$pdf->SetAutoPageBreak(TRUE, 5);
//Generating a random table for testing.
$table = [
['Name','Description','md5'],
];
$rows = rand(10,30);
//$rows = rand(3,5);
for($i = 0; $i < $rows; $i++) {
$table[] = array(
'Sector '.rand(1,10000),
str_repeat('An Apple', rand(2,6)),
md5(rand(1,100000)),
);
}
$pdf->addPage();
$pdf->Write(2, 'MultiCell Example');
$pdf->Ln();
$pdf->SetFont('times', '', 8);
$column_widths = array(
20,
60,
30,
);
//Total table should only be 10cm tall.
$maxheight = 100/count($table);
if($maxheight > 10) {
$maxheight = 10;
}
foreach($table as $index => $row) {
if($index == 0) {
$pdf->SetFillColor(230,230,255);
} else {
if( ($index&1) == 0 ) {
$pdf->SetFillColor(210,210,210);
} else {
$pdf->SetFillColor(255,255,255);
}
}
$pdf->SetX(10);
$currenty = $pdf->GetY();
foreach($row as $index => $column) {
$pdf->MultiCell(
$width = $column_widths[$index],
$minheight = $maxheight,
$text = $column,
$border = 'B', //Border bottom
$align = 'L',
$fill = true,
$ln = 0, //Move to right after cell.
$x = null,
$y = null,
$reseth = true,
$stretch = 0,
$ishtml = false,
$autopadding = true,
$maxheight,
$valign = 'T',
$fitcell = true);
}
$pdf->SetY($currenty + $maxheight);
}
$pdf->addPage();
$pdf->SetFont('times', '', 8);
$pdf->Write(2, 'Cell Example');
$pdf->Ln();
$pdf->SetFont('times', '', 8);
$maxheight = 100/count($table);
if($maxheight > 8) {
$maxheight = 8;
}
$maxfontsize = 10;
$pdf->SetFontSize($maxfontsize);
foreach($table as $index => $row) {
if($index == 0) {
$pdf->SetFillColor(230,230,255);
} else {
if( ($index&1) == 0 ) {
$pdf->SetFillColor(210,210,210);
} else {
$pdf->SetFillColor(255,255,255);
}
}
$pdf->SetX(10);
$currenty = $pdf->GetY();
foreach($row as $index => $column) {
//Reduce the font size to fit properly.
$pdf->SetFontSize($csize = $maxfontsize);
//0.2 step down font until it fits the cell.
while($pdf->GetStringWidth($column) > $column_widths[$index]-1 ) {
$pdf->SetFontSize($csize -= 0.2);
}
$pdf->Cell(
$width = $column_widths[$index],
$cellheight = $maxheight,
$text = $column,
$border = 'B', //Border bottom
$ln = 0,
$align = 'L',
$fill = true,
$stretch = 1,
$ignore_min_height = true,
$calign = 'T',
$valign = 'T');
}
$pdf->SetY($currenty + $maxheight);
}
$pdf->Output(dirname(__FILE__).'/maxheight.pdf', 'F');
附錄: WriteHTML
替代方法
使用WriteHTML
可以實現的一種方法是,使用startTransaction
啟動事務,為整個表設置基本字體並編寫HTML,然后檢查頁面數。 如果遇到自動分頁符,請回滾事務並嘗試使用較小的字體。 否則,提交事務。
作為示例,我已經使用上面的輸出更新了上面的鏈接:
//Example with WriteHTML.
$pdf->AddPage();
$pdf->SetFont('times', '', 8);
$pdf->Write(2, 'WriteHTML Example');
$pdf->Ln();
$pdf->SetFont('times', '', 8);
//Max height of 100 mm.
$maxy = $pdf->GetY()+100;
$fontsize = 14;
//Make table markup.
$tablehtml = '<table cellspacing="2" style="font-family: times; font-size:_FONTSIZE_px;">';
foreach($table as $index => $row) {
if($index == 0) {
$rowstyle = ' background-color: rgb(230,230,255); '.
'font-size: 110%; font-familt: times; font-weight: bold;'.
'border-bottom: 1px solid black;';
} else {
if( ($index&1) == 0 ) {
$rowstyle = 'background-color: rgb(210,210,210);';
} else {
$rowstyle = 'background-color: white;';
}
}
$tablehtml .= "<tr style=\"$rowstyle\">";
foreach($row as $column) {
$tablehtml .= "<td>$column</td>";
}
$tablehtml .= '</tr>';
}
$tablehtml .= '</table>';
//Transaction loop.
$numpages = $pdf->getNumPages();
$done = false;
while(!$done) {
$pdf->startTransaction(true);
$pdf->SetFont('times', '', 14);
$outtable = str_replace('_FONTSIZE_', $fontsize, $tablehtml);
$pdf->writeHTML($outtable);
if($pdf->getNumPages() > $numpages || $pdf->GetY() > $maxy) {
//If we encountered a page break or exceeded the desired maximum height
//rollback the transaction.
$pdf->rollbackTransaction(true);
$fontsize -= 0.4;
//Larger font steppings will be less precise, but faster.
} else {
$pdf->commitTransaction(true);
$done = true;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.