FPDF is a great tool to easily create PDF documents. The automatic line-breaking feature is great, but leaves something to be desired. For instance, if a MultiCell is too tall to fit in the remainder of a page, the element gets split up.
Unfortunately, there is no built-in method to calculate what the height of a MultiCell would be.
One solution to this issue posted on StackOverflow utilizes GetStringWidth() to try and approximate what the height would be. I tried it out, but it doesn’t quite hit the nail on the head. It would produce inaccurate results due to spacing and newline issues. I wanted an exact height, not an estimated guess.
I ended up cloning the MultiCell() function within the FPDF class, gutting it, and re-purposing it to return an accurate height. Every time the function made a call to Cell(), I replaced the call with a line of code to add the height of the Cell to the total height of the MultiCell. I also removed a few things such as any code relating to the border.
Using a conditional statement, I then checked the height of the MultiCell against the remaining space on the page:
$spaceleft = $pdf->h - $pdf->GetY() - $pdf->bMargin
if ($pdf->GetMultiCellHeight(args) > $spaceleft)
You can get a copy of my function here: https://gist.github.com/4089627. Don’t trust random code on the internet? I wouldn’t either. View the diff against the original MultiCell() function here: http://diffchecker.com/99S1TJ8b.