【问题标题】:Adjust font size FPDF调整字体大小 FPDF
【发布时间】:2012-09-18 15:06:54
【问题描述】:

在 FPDF 中,我有一个宽度为 176 毫米的单元格,我需要在其中输入客户名称。问题是客户端名称并不总是调整到该固定宽度。有没有办法让单元格的字体大小自动调整到单元格宽度,以防它太长?

这是我现在拥有的代码:

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

我知道 TCPDF 有一种设置自动拉伸的方法,但我还没有找到任何适用于 FPDF 的方法。我必须用代码来做吗?

【问题讨论】:

    标签: php pdf fpdf


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      好吧,原来有一个名为 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 );
      

      【讨论】:

      • $pdf->SetFont('Trebuchet', 'B', --$x); // 递减,然后在一行中设置新的字体大小
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多