【问题标题】:FPDF Text StrokeFPDF 文本描边
【发布时间】:2013-06-03 16:26:20
【问题描述】:

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

我该如何解决这个问题?

【问题讨论】:

    标签: php pdf text fpdf stroke


    【解决方案1】:

    问题出在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?

    【讨论】:

      【解决方案2】:

      有点晚了:

      我在自定义类中使用了函数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');
      

      结果

      【讨论】:

        猜你喜欢
        • 2022-07-21
        • 1970-01-01
        • 1970-01-01
        • 2016-11-18
        • 1970-01-01
        • 1970-01-01
        • 2021-10-27
        • 2014-09-02
        • 1970-01-01
        相关资源
        最近更新 更多