首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FPDF字母间距

FPDF字母间距
EN

Stack Overflow用户
提问于 2012-06-21 02:59:41
回答 5查看 13.5K关注 0票数 3

我正在尝试为fpdf中的特定“块”文本设置字母间距。我已经搜索了,并且只找到了一种方法来设置整个文档的字母间距,但即使这样也不起作用。文本被发布到php fpdf生成器。

代码语言:javascript
复制
$pdf->SetFont('Arial','b',85, LetterSpacing Here?);

有什么帮助吗?

EN

回答 5

Stack Overflow用户

发布于 2016-09-01 22:36:25

根据其他提供的答案,我扩展了我们使用的FPDF类,以便下划线考虑用户定义的字母间距。

代码语言:javascript
复制
<?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);
}
}

示例使用测试:

代码语言:javascript
复制
$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

票数 7
EN

Stack Overflow用户

发布于 2015-09-22 17:52:43

这将真正允许您进行字母间距:

代码语言:javascript
复制
// letter-spacing (0 for normal, 0.3 = 33%, 1 = 100%) 
function SetCharSpacing($cs) { 
    $this->_out(sprintf('BT %.3F Tc ET',$cs*$this->k)); 
}

致词:http://fpdf.de/forum/showthread.php?t=3241

票数 3
EN

Stack Overflow用户

发布于 2012-07-22 21:03:00

不幸的是,你不能只用函数直接做这个。这里你需要的是编写一个新的函数,用一些新的参数重新创建Cell() ...

等等..。已经有人这么做了!

在这里:FPDF Add-On by Patrick Benny

这是一份很棒的工作,你甚至不需要其他工作!:)

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11126354

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档