FPDF font size setting

In FPDF, I have a cell 176 mm wide where I need to put the client name. The problem is that the client name is not always set to this fixed width. Is there a way for the font size of the cell to automatically adjust to the width of the cell if it is too long?

This is the code that I have right now:

$pdf->Cell( 116, 7, utf8_decode( $row_or[ 'client_name' ] ), 0, 0, 'L' );

I know that TCPDF has a way to set auto-expansion, but I did not find it for FPDF. Should I do this with code?

+5
source share
2 answers

Well, it turns out that there is a GetStringWidth function that gets the string and returns its width in millimeters, so I did this:

/* I know that the font size starts with 11, so i set a variable at this size */
$x = 11;    // Will hold the font size
/* I will cycle decreasing the font size until it width is lower than the max width */
while( $pdf->GetStringWidth( utf8_decode( $row_or[ 'client_name' ] ) ) > 116 ){
    $x--;   // Decrease the variable which holds the font size
    $pdf->SetFont( 'Trebuchet', 'B', $x );  // Set the new font size
}
/* Output the string at the required font size */
$pdf->Cell( 116, 7, utf8_decode( $row_or[ 'client_name' ] ) ), 0, 0, 'L' );
/* Return the font size to itΕ› original */
$pdf->SetFont( 'Trebuchet', 'B', 11 );
+7
source

, : $ = 0,1; $x -;

0

All Articles