简体   繁体   English

调整字体大小FPDF

[英]Adjust font size FPDF

In FPDF I have a cell with a width of 176mm where I need to put a client name. 在FPDF中,我有一个宽度为176mm的单元格,我需要放置一个客户端名称。 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. 我知道TCPDF有一种设置自动拉伸的方法,但我没有找到任何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: 好吧,事实证明有一个名为GetStringWidth的函数接收一个字符串并以毫米为单位返回它的宽度,所以,我做的是:

/* 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; 对于更精细的列调整,减少量也可以是分数,例如:$ x- = 0.1; instead of $x--; 而不是$ x--;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM