问题
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?
回答1:
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
回答2:
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! :)
回答3:
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
回答4:
Place it in your fpdf php class.
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 ) );
}
before that add global variable in fpdf class; var $FontSpacingPt;
Hope it helps with lattest fpdf class.
来源:https://stackoverflow.com/questions/11126354/fpdf-letter-spacing