调整字体大小FPDF

时间:2012-09-26 21:55:13

标签: php pdf fpdf

在FPDF中,我有一个宽度为176mm的单元格,我需要放置一个客户端名称。问题是客户端名称始终不会调整到该固定宽度。有没有办法让单元格的字体大小自动调整到单元格宽度,以防它太长?

这是我现在的代码:

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

我知道TCPDF有办法设置自动拉伸,但我没有找到任何FPDF。我是否必须使用代码进行操作?

2 个答案:

答案 0 :(得分:9)

好吧,事实证明有一个名为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 );

答案 1 :(得分:1)

对于更精细的列调整,减少量也可以是分数,例如:   $ X- = 0.1; 而不是$ x - ;

相关问题