Break line in Multi Cell FPDF with various text properties

Viewed 97

I'm trying to make a box which contains 2 lines of text which have their own text properties using FPDF library :

I know i can use the multicell here, but how about text property like text size and font family ?

This is my current code :

public function print()

{
    $this->fpdf->AddPage('L',[100,150]);
    
    $this->fpdf->SetFont('Arial','B',16);
    $this->fpdf->MultiCell(130,8,utf8_decode('Material description :' . chr(10) . 'V-001940 LCMS AA001'),1);
    
    $this->fpdf->Output();
}

my current output :

current output

expected output :

expected output

Is it possible to do this ?

and also its strange that when im using the '\n' to insert breaks, nothing happened, hence i need to use this line of code instead :

utf8_decode('Material description :' . chr(10) . 'V-001940 LCMS AA001')

What is the solution ?

1 Answers

FPDF supports printing text with line breaks, however the entire string of text must use the same text properties (text size and font family).

You can extend to add a new function:

class RPDF extends \FPDF {

    private function MultiLineCell($w,$h,$text1,$text2,$border,$ln,$align,$fill, $font1, $fontst1, $fontsz1, $font2, $fontst2, $fontsz2)
    {
        // Store reset values for (x,y) positions
        $x = $this->GetX();
        $y = $this->GetY() + $h;

        $this->SetFont($font1, $fontst1, $fontsz1);
        // Make a call to FPDF's MultiCell
        $this->MultiCell($w,$h,$text1,$border,$align,$fill);
        // Reset the line position to the right, like in Cell
        if( $ln==0 )
        {
            $this->SetXY($x,$y);
        }
        $this->SetFont($font2, $fontst2, $fontsz2);
        $this->MultiCell($w,$h,$text2,$border,$align,$fill);
    }
}
Related