FPDF - MultiCell height determination before placement?

The main question is: is it possible to determine the height of the MultiCell before it is placed in the document?

Reason: I was tasked with creating a PDF version of the form. This form allows you to enter text with the resulting variable length. One person whom I did not enter, another person can write several paragraphs. "The Powers That Be" does not want this text to break between pages.

Currently, after placing each block, I check the position on the page, and if I'm close to the end, I create a new page.

if($this->getY() >= 250) {
    $this->AddPage('P');
}

For the most part this works. But there are a few vile ones that come in, say, 249, and then have a boat of text. Apparently, it would be wiser to determine the height of the block before placing it to see if it really fits on the page.

Of course, there should be a way to do this, but googling around is not very useful.

Edit to clarify: PDF is created after the form is submitted. Inactive, editable PDF form ...

+5
source share
3 answers

Earned a crawl icon for this question. :( Anyway, by chance, someone has the same problem, I decided that I would answer what I did. I doubt that this is the most effective method, but it did its job.

First, I create two FPDF objects; temporary, which will never be displayed, and final, complete pdf, which the end user will see.

Y . , Cell() MultiCell() ( , ) , Y. , . -. , . .

, , Y , ( ), . , , , .

.

, , - - , , . ( ...)

+4

. https://gist.github.com/johnballantyne/4089627

function GetMultiCellHeight($w, $h, $txt, $border=null, $align='J') {
    // Calculate MultiCell with automatic or explicit line breaks height
    // $border is un-used, but I kept it in the parameters to keep the call
    //   to this function consistent with MultiCell()
    $cw = &$this->CurrentFont['cw'];
    if($w==0)
        $w = $this->w-$this->rMargin-$this->x;
    $wmax = ($w-2*$this->cMargin)*1000/$this->FontSize;
    $s = str_replace("\r",'',$txt);
    $nb = strlen($s);
    if($nb>0 && $s[$nb-1]=="\n")
        $nb--;
    $sep = -1;
    $i = 0;
    $j = 0;
    $l = 0;
    $ns = 0;
    $height = 0;
    while($i<$nb)
    {
        // Get next character
        $c = $s[$i];
        if($c=="\n")
        {
            // Explicit line break
            if($this->ws>0)
            {
                $this->ws = 0;
                $this->_out('0 Tw');
            }
            //Increase Height
            $height += $h;
            $i++;
            $sep = -1;
            $j = $i;
            $l = 0;
            $ns = 0;
            continue;
        }
        if($c==' ')
        {
            $sep = $i;
            $ls = $l;
            $ns++;
        }
        $l += $cw[$c];
        if($l>$wmax)
        {
            // Automatic line break
            if($sep==-1)
            {
                if($i==$j)
                    $i++;
                if($this->ws>0)
                {
                    $this->ws = 0;
                    $this->_out('0 Tw');
                }
                //Increase Height
                $height += $h;
            }
            else
            {
                if($align=='J')
                {
                    $this->ws = ($ns>1) ? ($wmax-$ls)/1000*$this->FontSize/($ns-1) : 0;
                    $this->_out(sprintf('%.3F Tw',$this->ws*$this->k));
                }
                //Increase Height
                $height += $h;
                $i = $sep+1;
            }
            $sep = -1;
            $j = $i;
            $l = 0;
            $ns = 0;
        }
        else
            $i++;
    }
    // Last chunk
    if($this->ws>0)
    {
        $this->ws = 0;
        $this->_out('0 Tw');
    }
    //Increase Height
    $height += $h;

    return $height;
}
+2

JLuc, , . , fpdf.

, fpdf, JLuc . , :

, FPDF ( ):

<?php
namespace YourNameSpace\Library;

class FpdfExtended extends \FPDF {
...
}

, require. GetMultiCellHeight .

, , , .

use YourNameSpace\Library\FpdfExtended
...
$pdf = new FpdfExtended();
...

GetMultiCellHeight :

$col1 = 'col 1 text without line break';
$col2 = 'col 2 text with line break \n and some more text';

// get next cell height
$nextHeight = $pdf->GetMultiCellHeight($width, $height, $col2);

$pdf->MultiCell($width, $nextHeight, $col1, 0, 'L', 1);

Good luck and happy coding to you poor souls still using FPDF.

0
source

Source: https://habr.com/ru/post/1524147/


All Articles