FPDF字母间距 [英] FPDF Letter Spacing
本文介绍了FPDF字母间距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试为fpdf中的特定文本块"设置字母间距.我进行了搜索,但只找到了一种为整个文档设置字母间距的方法,即使那样也没有用.文本已发布到php fpdf生成器.
I am trying to set the letterspacing for a specific 'block' of text in fpdf. I have searched and have only found one way to set the letterspacing for the whole doc, and even that didn't work. The text is posted to the php fpdf generator.
$pdf->SetFont('Arial','b',85, LetterSpacing Here?);
有帮助吗?
推荐答案
基于其他提供的答案,我扩展了我们使用的FPDF类,以便在下划线时考虑用户定义的字母间距.
Based on other provided answers, I extended the FPDF class that we use so that underlining takes into account user-defined letter spacing.
<?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
Tested extending FPDF version 1.81
这篇关于FPDF字母间距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文