FPDF - Determining height of MultiCell before placing?

后端 未结 3 1496
不知归路
不知归路 2021-02-14 07:48

The basic question: Is it possible to determine the height of a MultiCell before placing it in the document?

The reason: I\'ve been tasked with creating a PDF version of

3条回答
  •  广开言路
    2021-02-14 08:13

    In addition to JLuc answer which is working as one would imagine it, too. Implementing this functionality however is not trivial and requires you to extend the main fpdf class.

    First off if you have an extended fpdf already you could just copy JLuc's answer and skip this step. If not, go ahead like this:

    create a File and Class that extends FPDF (name the file as your class name):

    If you are not using namespaces you could just use require. Once that file is created insert the GetMultiCellHeight from the gist or this answer.

    Now you'd need to instantiate the extended Class a simple constructor most likely suffices.

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

    now you could simply use the GetMultiCellHeight method to decide the next cells height like so:

    $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 you poor souls still using FPDF.

提交回复
热议问题