FPDF文本笔划

时间:2013-06-03 16:26:20

标签: php pdf text fpdf stroke

我正在尝试使用FPDF PHP类在PDF文件中编写带有笔划的文本。

我注意到一个奇怪的黑色文字边框,我不知道如何让它消失。

我将向您展示我正在做的事情和结果的简化代码,以便您了解我的问题:

require('fpdf/fpdf.php');
$pdf = new FPDF('P','pt',array(250,300));
$pdf->AddPage();

$letter = imagecreatetruecolor(250,300);
imagealphablending($letter, 1);
imagesavealpha($letter, 1);
imagefill($letter, 0, 0, imagecolorallocatealpha($letter, 0, 0, 0, 127));

$border = imagecreatetruecolor(250,300);
imagealphablending($border, 1);
imagesavealpha($border, 1);
imagefill($border, 0, 0, imagecolorallocatealpha($border, 0, 0, 0, 127));

$letter_color = imagecolorallocate($letter, 0, 0, 255);
$border_color = imagecolorallocate($letter, 255, 0, 0);

imagettftext($letter, 350, 0, 25, 250, $letter_color, 'font/times.ttf', 'a');
imagettftext($border, 350, 0, 25, 250, $border_color, 'font/times.ttf', 'a');

imagepng($letter,'letter.png');
imagepng($border,'border.png');

imagedestroy($letter);
imagedestroy($border);

for($j = 0; $j < 10; $j++) {
  for($k = 0; $k < 10; $k++) {
    $pdf->Image('border.png', $k - 5, $j - 5, 250, 300);
  }
}

$pdf->Image('letter.png', 0, 0, 250, 300);

unlink('letter.png');
unlink('border.png');

$pdf->Output();

结果如下:http://postimg.org/image/l97rr0xzr/

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

问题出在imagettftext(),但是如果我使用该功能在单个图像中打印相同颜色的字母,则阴影仅出现在“合成字母”的边框上。现在我暂时移动for循环来执行我刚才解释的内容。

这是简化的代码:

require('fpdf/fpdf.php');
$pdf = new FPDF('P','pt',array(250,300));
$pdf->AddPage();

$letter = imagecreatetruecolor(250,300);
imagealphablending($letter, 1);
imagesavealpha($letter, 1);
imagefill($letter, 0, 0, imagecolorallocatealpha($letter, 0, 0, 0, 127));

$letter_color = imagecolorallocate($letter, 0, 0, 255);
$border_color = imagecolorallocate($letter, 255, 0, 0);

for($j = -5; $j <= 5; $j++) {
  for($k = -5; $k <= 5; $k++) {
    imagettftext($letter, 350, 0, 25 + $j, 250 + $k, $border_color, 'font/times.ttf', 'a');
  }
}

imagettftext($letter, 350, 0, 25, 250, $letter_color, 'font/times.ttf', 'a');
imagepng($letter,'letter.png');
imagedestroy($letter);

$pdf->Image('letter.png', 0, 0, 250, 300);
unlink('letter.png');
$pdf->Output();

结果如下:http://postimg.org/image/s44v2rbqn/

仍在寻找如何删除阴影。

现在我真的明白了这个问题,我发现这是重复的。遗憾。

如果你知道答案,请点击此处:How we can change the font opacity and shadowing in imagettftext function?

答案 1 :(得分:0)

有点晚了:

我在自定义类中使用了函数http://www.fpdf.org/en/script/script78.php#

class CustomPdf extends Fpdf
{

    public function ClippingText($x, $y, $txt, $outline=false)
    {
        $op= $outline ? 5 : 7;
        $this->_out(sprintf('q BT %.2F %.2F Td %d Tr (%s) Tj ET',
            $x*$this->k,
            ($this->h-$y)*$this->k,
            $op,
            $this->_escape($txt)));
    }


    public function strokeText($x, $y, $txt, $outline=true)
    {
        $this->SetTextColor(46, 52, 120);
        $this->Text($x,$y,$txt);
        $this->SetDrawColor(186, 236, 253);
        $this->SetLineWidth(0.5);
        $this->ClippingText($x,$y,$txt,$outline);
    }
}

$pdf = new CustomPdf();
.... add pages and fonts and cells
$pdf->strokeText(30,30,'Hola mundo');

结果

limonazzo.com