簡體   English   中英

調整字體大小FPDF

[英]Adjust font size FPDF

在FPDF中,我有一個寬度為176mm的單元格,我需要放置一個客戶端名稱。 問題是客戶端名稱始終不會調整到該固定寬度。 有沒有辦法讓單元格的字體大小自動調整到單元格寬度,以防它太長?

這是我現在的代碼:

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

我知道TCPDF有一種設置自動拉伸的方法,但我沒有找到任何FPDF。 我必須用代碼嗎?

好吧,事實證明有一個名為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 );

對於更精細的列調整,減少量也可以是分數,例如:$ x- = 0.1; 而不是$ x--;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM