php - FPDF 字母间距

标签 php fpdf

我正在尝试为 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/

相关文章:

php - 如何在 php 中使用 fpdf 打破 mutlicell 中的行?

php - fpdf: header 在 fpdf 中不起作用

php - 压缩图表的测量数据,存储在 MySQL 中

php - 如何维护聊天数据?

php - 只过滤掉一些与SQL有共同点的参数

php - 如何获取一个月内的所有日期

php - 使用 FPDF 在图像上写入文本

php - 自动分页 FPDF

php - 如何暂时跳过一行

php - 使用 fopen 和 curl_setopt 的 HTTP 请求