Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adjust font size FPDF

Tags:

php

pdf

fpdf

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?

like image 914
Tales Avatar asked Sep 26 '12 21:09

Tales


People also ask

How do you fit text in a cell in Fpdf?

$w_w=$c_height/3; example:if you want to wrap a word with 3 line. and the cell height is 9 so 9/3=3. first w_w line will be echo in height 3 and next line is echoed in height 6 and next line will be in height 9.

How do you change the font color in Fpdf?

$pdf->SetTextColor(255,255,255); $pdf->Cell(50,0,'WHITE ORANGE ORANGE WHITE',0,1,'C');


1 Answers

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 );
like image 165
Tales Avatar answered Sep 23 '22 00:09

Tales