TCPDF - 多单元格单元高度

时间:2015-12-03 12:44:45

标签: php pdf-generation tcpdf

我正在尝试在TCPDF中创建发票,但我无法创建表。表格单元格必须包含Word Wrapping。我引用了很多网站如何做到这一点,但我无法做到这一点。

问题出在第二行,我可以让细胞排队。 Cell height isproblem http://apartman-donat.com/Capture.JPG

这是我的代码:

$pdf->ln(20);
$w = array(10, 80, 15, 25,30,35);
$pdf->SetFillColor(127, 127, 127);
$pdf->SetTextColor(255,255,255);
$pdf->SetDrawColor(127, 127, 127);
$pdf->SetLineWidth(0.3);
$pdf->SetFont($fontname,'',12);
$pdf->Cell($w[0], 10, '#', 1, 0, 'C', 1);
$pdf->Cell($w[1], 10, 'OPIS USLUGE', 1, 0, 'C', 1);
$pdf->Cell($w[2], 10, 'J.mj.', 1, 0, 'C', 1);
$pdf->Cell($w[3], 10, 'Količina', 1, 0, 'C', 1);
$pdf->Cell($w[4], 10, 'Cijena', 1, 0, 'C', 1);
$pdf->Cell($w[5], 10, 'Iznos [Kn]', 1, 0, 'C', 1);
$pdf->ln();
$pdf->SetFillColor(255, 255, 255);
$pdf->SetTextColor(0,0,0);
$pdf->SetDrawColor(127, 127, 127);
$pdf->SetLineWidth(0.3);
$pdf->SetFont($fontname,'',8);
//$pdf->setFontSpacing(0);
//$pdf->setCellHeightRatio(0.8);
//$pdf->MultiCell($w, $h, $txt, $border, $align, $fill, $ln, $x, $y, $stretch, $ishtml, $autopadding, $valign, $fitcell);
$pdf->MultiCell($w[0], 15, '1', 1, 'C', false, 0, '', '', false, 0, false, false,  15,'T', false);
$pdf->MultiCell($w[1], 15, 'Energetski pregled i izrada energetskog certifikata stana na adresi: Petrova ul. 33, 10000 Zagreb', 1, 'L', false, 0, '', '', false, 0, false, false,  0,'T', false);
$pdf->MultiCell($w[2], 15, 'kom', 1, 'C', false, 0, '', '', false, 0, false, false,  15,'B', false);
$pdf->MultiCell($w[3], 15, '1', 1, 'C', false, 0, '', '', false, 0, false, false, 15,'B', false);
$pdf->MultiCell($w[4], 15, '600.00', 1, 'C', false, 0, '', '', false, 0, false, false,  15,'B', false);
$pdf->MultiCell($w[5], 15, '600.00', 1, 'C', false, 0, '', '', false, 0, false, false, 15,'B', false);

我做错了什么?

2 个答案:

答案 0 :(得分:1)

首先,您需要根据MultiCell尺寸设置设置/估算width功能每行最多可显示的字符数。例如,只说了大约48个字符。

$textlength = strlen('Energetski pregled i izrada energetskog certifikata stana na adresi: Petrova ul. 33, 10000 Zagreb');

$rowheight = 15;
if ($textlength > 48){ // alocate as you need
  if ($textlength > 144){
    $rowheight = $rowheight *4;
  } else if ($textlength > 96){
    $rowheight = $rowheight *3;
  } else {
    $rowheight = $rowheight *2;
  }
}


    $pdf->MultiCell($w[0], $rowheight, '1', 1, 'C', false, 0, '', '', false, 0, false, false,  15,'T', false);
    $pdf->MultiCell($w[1], 15, 'Energetski pregled i izrada energetskog certifikata stana na adresi: Petrova ul. 33, 10000 Zagreb', 1, 'L', false, 0, '', '', false, 0, false, false,  0,'T', false);
    $pdf->MultiCell($w[2], $rowheight, 'kom', 1, 'C', false, 0, '', '', false, 0, false, false,  15,'B', false);
    $pdf->MultiCell($w[3], $rowheight, '1', 1, 'C', false, 0, '', '', false, 0, false, false, 15,'B', false);
    $pdf->MultiCell($w[4], $rowheight, '600.00', 1, 'C', false, 0, '', '', false, 0, false, false,  15,'B', false);
    $pdf->MultiCell($w[5], $rowheight, '600.00', 1, 'C', false, 0, '', '', false, 0, false, false, 15,'B', false);

答案 1 :(得分:1)

有一个函数getNumLines(),您可以使用它来计算特定宽度所需的行数:

$numberOfLines = $pdf->getNumLines($text, $width);

您可以使用它来计算每一行的高度。