如何从 fpdf 中的 pdf 锚点 link 中删除下划线

How to remove Underline from anchor link of a pdf in fpdf

我们正在使用 FPDF 制作 pdf 报告,其中我们有 links。

我们的问题是我们无法从我们在 pdf 报告中发送的 link 中删除下划线和默认颜色。我们想在不同的 link 上设置自定义的不同颜色。

下面是我们正在做的。

$text22 = preg_replace('/\S*\b('. $searchphrase[$rr] .')\b\S*/i', '$1 ', $aaa);

$text22 = preg_replace('/\S*\b('. $searchphrase[$rr] .')\b\S*/i', '', $aaa);

$pdf->WriteHTML(utf8_decode($main));

下面是我们的 pdf 报告,现在我们必须从 link 中删除下划线并为其设置自定义颜色。

您可能需要自己扩展 FPDF class 并更改 PutLink 函数中的下划线/颜色。

http://www.fpdf.org/en/script/script50.php

编辑

这是扩展 FPDF 的代码示例 class。事实上,由于 WriteHTML 函数不在 FPDF class 中,因此它扩展了包含它的 class。这是使其工作的众多方法之一。在此示例中,您必须在附加属性 (data-color) 中指定 link 颜色,因为 class 无法读取 CSS 规则。您当然可以编写一个正则表达式来解析 CSS,然后将颜色转换为 r、g、b。但是为了一个更简单的例子,我把它省略了。

<?php

// This class extends the Tutorial 6 class, which in turn extends the main FPDF class
class XPDF extends PDF
{
  protected $clr = "";

  function OpenTag($tag, $attr)
  {
   parent::OpenTag($tag, $attr);
    if ($tag == "A")
    {
      if (isset($attr['DATA-COLOR']))
      {
        
        $this->clr = $attr['DATA-COLOR'];  
      }
      else
      {
        $this->clr = "";
      }
    }
  }
  
  function CloseTag($tag)
  {
    parent::CloseTag($tag);
    if ($tag == "A")
      $this->clr = "";
  }

  function PutLink($URL, $txt)
  {
      // Put a hyperlink
      if ($this->clr != "")
      {
        $clrs = explode(",", $this->clr);
        $this->SetTextColor($clrs[0], $clrs[1], $clrs[2]);
      }
      else
      {
        $this->SetTextColor(0,0,255);
      }
   $this->SetStyle('U',true);
   $this->Write(5,$txt,$URL);
   $this->SetStyle('U',false);
   $this->SetTextColor(0);
  }
}

$html = 'This is some text and <a href="http://www.whatever.com">here is a link</a>.  To specify the pdf colour of a link, you need to specify it as RGB numbers in a data-attribute, like <a href="http://www.whatever.com" data-color="255,0,0">this</a> or <a href="http://www.whatever.com" data-color="255,0,100">this</a>.';

$pdf = new XPDF();
// First page
$pdf->AddPage();
$pdf->SetFont('Arial','',14);
$pdf->WriteHTML($html);
$pdf->Output();
?>