我正在尝试为 fpdf 中的特定文本“ block ”设置字母间距。我已经搜索过,但只找到了一种为整个文档设置字母间距的方法,即使这样也没有用。文本被发布到 php fpdf 生成器。
$pdf->SetFont('Arial','b',85, LetterSpacing Here?);
有什么帮助吗?
最佳答案
根据其他提供的答案,我扩展了我们使用的 FPDF 类,以便下划线考虑用户定义的字母间距。
<?php
class Custom_FPDF extends FPDF
{
protected $FontSpacingPt; // current font spacing in points
protected $FontSpacing; // current font spacing in user units
function SetFontSpacing($size)
{
if($this->FontSpacingPt==$size)
return;
$this->FontSpacingPt = $size;
$this->FontSpacing = $size/$this->k;
if ($this->page>0)
$this->_out(sprintf('BT %.3f Tc ET', $size));
}
protected function _dounderline($x, $y, $txt)
{
// Underline text
$up = $this->CurrentFont['up'];
$ut = $this->CurrentFont['ut'];
$w = $this->GetStringWidth($txt)+$this->ws*substr_count($txt,' ')+(strlen($txt)-1)*$this->FontSpacing;
return sprintf('%.2F %.2F %.2F %.2F re f',$x*$this->k,($this->h-($y-$up/1000*$this->FontSize))*$this->k,$w*$this->k,-$ut/1000*$this->FontSizePt);
}
}
示例使用测试:
$pdf = new Custom_FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial', 'BU', 11);
$pdf->SetFontSpacing(3);
$pdf->Cell(0, 10, 'Test of letter spacing with underline', 0, 1);
$pdf->SetFontSpacing(0);
$pdf->Cell(0, 10, 'Test of letter spacing with underline');
$pdf->Output();
测试扩展 FPDF 版本 1.81
关于php - FPDF 字母间距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11126354/