2

So, my problem is that i'm using FPDF to create a pdf file from php. Just one problem. Once text is too large for a cell it doesn't wrap. So i came to the point to try to use multicells, but there is another problem. Once a cell is wrapped in a table i got no way to get the other multicells to the same height.

Here is the code i tested.

<?php
require('../fpdf181/fpdf.php');

$pdf = new FPDF('P', 'mm', 'A4');

$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Arial', '', 14);

$x = $pdf->GetX();
$y = $pdf->GetY();
$push_right = 0;

$pdf->MultiCell(50,10,"TEST shdfkjhdsafhahsjdkfkhjshakjfhdsdsfhkjdkjhsafhkjdakjhsfhkjdskjhaf", "TBRL");

$pdf->SetXY($x+50, $y);

$pdf->MultiCell(50,10,"TEST shdfkjhdsafhahsjdkfkhjshakjfhdsdsfhkjdsafsdafdsafsdafsdafddkjhsafhkjdakjhsfhkjdskjhaf", "TBRL");

$pdf->Output();

From that code i got this:

enter image description here

But it should look like this:

enter image description here

Dave
  • 5,108
  • 16
  • 30
  • 40
Florian7843
  • 303
  • 3
  • 12
  • Does https://stackoverflow.com/questions/18356118/fpdf-height-of-a-multicell-element help? – Chris Nov 28 '18 at 19:01
  • No, because he only uses one multicell so he can just fetch height, but this is what i can't, because i want to use multiple multicells in one row. – Florian7843 Nov 28 '18 at 19:05
  • OK, I have overcome this before by using MultiCell for the text. Don't add a border. Write some code to get the Y position at the end of each MultiCell. Once you know this then you can draw lines to simulate what borders would have looked like. – Chris Nov 28 '18 at 19:54
  • That could work. I'll try that later – Florian7843 Nov 29 '18 at 06:30

2 Answers2

9

This is how it works, for those who have the same problem:

function MultiCellRow($cells, $width, $height, $data, $pdf)
{
    $x = $pdf->GetX();
    $y = $pdf->GetY();
    $maxheight = 0;

    for ($i = 0; $i < $cells; $i++) {
        $pdf->MultiCell($width, $height, $data[$i]);
        if ($pdf->GetY() - $y > $maxheight) $maxheight = $pdf->GetY() - $y;
        $pdf->SetXY($x + ($width * ($i + 1)), $y);
    }

    for ($i = 0; $i < $cells + 1; $i++) {
        $pdf->Line($x + $width * $i, $y, $x + $width * $i, $y + $maxheight);
    }

    $pdf->Line($x, $y, $x + $width * $cells, $y);
    $pdf->Line($x, $y + $maxheight, $x + $width * $cells, $y + $maxheight);
}

To execute the function I used: MultiCellRow(3, 50, 10, ["Cell1","Cell2", "Cell3"], $pdf);

Florian7843
  • 303
  • 3
  • 12
0

The accepted answer works for non-colored backgrounds. If, you wanted to have colored backgrounds, then the accepted answer wouldn't shade the smaller height columns properly.

The below code provides the same functionality as the approved answer, but also supports colored backgrounds. It may not be the cleanest solution (because it has to render the MultiCell components twice), but is the only solution I could create that actually works:

function MultiCellRow($pdf, $data, $width, $height,$darkenBackground){

$x = $pdf->GetX();
$y = $pdf->GetY();
$maxheight = 0;

for ($i = 0; $i < count($data); $i++) {
    $pdf->MultiCell($width, $height, $data[$i],0,'C');
    if ($pdf->GetY() - $y > $maxheight) $maxheight = $pdf->GetY() - $y;
    $pdf->SetXY($x + ($width * ($i + 1)), $y);
}

for ($i = 0; $i < count($data); $i++) {
    
    if($darkenBackground) $pdf->Rect($x+$width*$i,$y,$width,$maxheight,"F");
    $pdf->Line($x + $width * $i, $y, $x + $width * $i, $y + $maxheight);

    $pdf->SetXY($x+$i*$width,$y);
    $pdf->MultiCell($width, $height, $data[$i],0,'C');
}

$pdf->Line($x + $width * count($data), $y, $x + $width * count($data), $y + $maxheight);
$pdf->Line($x, $y, $x + $width * count($data), $y);
$pdf->Line($x, $y + $maxheight, $x + $width * count($data), $y + $maxheight);

$pdf->SetY($y+$maxheight);}

Where the inputs are:

  • $pdf is the pdf object (new FPDF();)
  • $data is the array of strings to be rendered in the row
  • $width is the cell width (integer)
  • $height is determines the padding/line-spacing of the cell
  • $darkenBackground is a Boolean.

I give partial credit to "Florian7843" for the first half of the code. I would have edited their existing post, but I made significant changes and thought it would be better to contribute a separate answer.

If somebody wants to dedicate a cleaner/efficient solution, please propose an edit.

Cheers!

G M
  • 311
  • 1
  • 5
  • 11