0

I Created a table with FPDF and I want to wrap the word in the cells(table). this is my code .

$w = array(7, 150, 40);
$pdf->Cell($w[0], 6, $count, 'LR');
$pdf->Cell($w[1], 6, $column_heading, 'LR');
$pdf->Cell($w[2], 6, $rekey, 'LR', 0, 'R');

$pdf->Ln();
$pdf->Cell(array_sum($w), 0, '', 'T');
$count++;

This is out put of the code how can I break the text in cells.

enter image description here

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

2 Answers2

0

FPDF prints the cells and the text separately (means that it's like 2 layers)

from manual: http://www.fpdf.org/

Cell(float w [, float h [, string txt [, mixed border [, int ln [, string align [, boolean fill [, mixed link]]]]]]])

Suggested Approach:

context length should be computed code-wise, if the content is greater than the allotted cell width then it should be divided and transferred to a new line.

At the same time cell height h should be adjusted (increase in height).

As I remember (fpdf experts can correct me) automatic adjustment of cells based on content is not yet implemented as a feature of the library.

Correction: there is http://www.fpdf.org/en/doc/multicell.htm

catzilla
  • 1,901
  • 18
  • 31
0

If you cannot use multicell, you could try shrinking the text until it fits in the cell by getting the string width.

example:

//loop the data
foreach($data as $item){
    $pdf->Cell(10, 5,$item[0],1,0);
    $pdf->Cell(60, 5,$item[1],1,0);

    //shrink font size until it fits the cell width
    while($pdf->getStringWidth($item[2]) > $cellWidth){// loop until the string width is smaller than cell width
        $pdf->SetFontSize($tempFontSize -= 0.1);
    }
    $pdf->Cell($cellWidth,5,$item[2],1,0);
    //reset font size to standard
    $tempFontSize = $fontSize;
    $pdf->SetFontSize($fontSize);

    $pdf->Cell(40,5,$item[3],1,1);
}
hello world
  • 306
  • 1
  • 6
  • 28