【问题标题】:TCPDF - Multicell cell heightTCPDF - 多单元格高度
【发布时间】:2016-03-08 02:15:08
【问题描述】:

我正在尝试在 TCPDF 中创建发票,但我无法创建表格。表格单元格必须有自动换行。我阅读了大量的网站如何做到这一点,但我无法做到这一点。

问题出在第二行,我可以让单元格对齐。 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);

我做错了什么?

【问题讨论】:

    标签: php pdf-generation tcpdf


    【解决方案1】:

    首先,您需要根据您的width 大小设置来设置/估计您的MultiCell 函数中每行最多可以显示多少个字符。例如刚才说的大约 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);
    

    【讨论】:

    • 现在,getStringHeight 可能会更整洁。
    【解决方案2】:

    有一个函数getNumLines() 允许您计算特定宽度所需的行数:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-07
      • 1970-01-01
      • 1970-01-01
      • 2011-12-31
      • 1970-01-01
      相关资源
      最近更新 更多