Reputation: 121
I am trying to create invoice in TCPDF, but I cant create table. Table cells must have Word Wrapping. I readed tons of site how to do that but I cant get it right.
Problem is with second row, I can get cells line up. Cell height isproblem http://apartman-donat.com/Capture.JPG
Here is my code:
$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);
What am I doing wrong?
Upvotes: 2
Views: 8288
Reputation: 13329
There is a function getNumLines()
which allow you to compute the number of lines needed for a particular width:
$numberOfLines = $pdf->getNumLines($text, $width);
You can use it for computing the height of each row.
Upvotes: 1
Reputation: 757
first you need to set/estimate how many characters maximum can show per line on your MultiCell
function according to your width
size setting. For example just said about 48 characters.
$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);
Upvotes: 5