FPDF字母间距[英] FPDF Letter Spacing

本文是小编为大家收集整理的关于FPDF字母间距的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在尝试为FPDF中特定的文本"块"设置字母序列.我已经搜索了,只找到了为整个文档设置字母序列的一种方法,即使是不起作用.该文本已发布到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

其他推荐答案

这确实可以让您进行字母间距:

// 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

其他推荐答案

不幸的是您不能直接使用FPDF函数直接执行.您需要的是用一些新参数编码新功能wich Repreate Cell() ...

但是等等... 有人已经做到了!

在这里: patrick benny

这是一项出色的工作,您甚至不需要其他东西! :)

本文地址:https://www.itbaoku.cn/post/2090904.html

问题描述

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?);

Any help?

推荐答案

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);
}
}

Sample use test:

$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();

Tested extending FPDF version 1.81

其他推荐答案

This will really allow you to do letter spacing:

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

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

其他推荐答案

Unfortunately you can't do that directly with only FPDF functions. What you need here is to code a new function wich recreate Cell() with some new parameters...

But wait... someone already did that!

It's here: FPDF Add-On by Patrick Benny

It's such a great job that you don't even need something else! :)