In FPDF I have a cell with a width of 176mm where I need to put a client name. The problem is that the client name doesn always adjust to that fixed width. Is there a way to make the font size of the cell autoadjust to the cell width in case 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 the auto-stretch but i have not found any for FPDF. Do I have to do it with code?
Well, it turns out that there is a function called GetStringWidth which receives a string and returns itś width in milimeters, so, what i did was:
/* 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's 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 );
The decrease can be fractions of points too for finer columns adjustment, like: $x-=0.1; instead of $x--;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.