2017-01-28 127 views
-1

我們正在使用FPDF製作PDF報告,其中包含鏈接。如何從fpdf的pdf鏈接中刪除下劃線

我們的問題是,我們無法從我們在pdf報告中發送的鏈接中刪除下劃線和默認顏色。我們希望在不同的鏈接上設置自定義的不同顏色。

以下是我們正在做的。

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

$ text22 = preg_replace函數( '/ \ S * \ B(' $ searchphrase [$ RR ')\ B \ S */I', '$ 1',$ AAA);

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

下面是我們的pdf報告,現在我們必須從鏈接中刪除下劃線並設置自定義顏色。

enter image description here

回答

0

您可能需要自己擴展FPDF類,並更改PutLink功能下劃線/顏色。

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

編輯

這裏的擴展FPDF類的代碼示例。實際上,由於WriteHTML函數不在FPDF類中,所以它擴展了它的類。這是使其工作的許多方法之一。在這個例子中,你必須在一個附加屬性中指定鏈接顏色(data-color),因爲該類不能讀取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) 
 
    { 
 
\t 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); 
 
     } 
 
\t $this->SetStyle('U',true); 
 
\t $this->Write(5,$txt,$URL); 
 
\t $this->SetStyle('U',false); 
 
\t $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(); 
 
?>

+0

我們已經取得通過錨標記的聯繫,但我們要刪除下劃線,改變定位標記的顏色。好意提示。 – Harinarayan

+0

是的。查看擴展FPDF類並重寫PutLink函數。讓我知道你不明白這一點,我會澄清。 –

+0

$ text22 = preg_replace('/ \ S * \ b('。$ searchphrase [$ rr]。')\ b \ S */i','$1',$ aaa);這個代碼當我們在瀏覽器上運行的時候運行良好,但是當我們在PDF中傳遞$ text22作爲$ pdf-> WriteHTML(utf8_decode($ text22)); CSS風格不起作用。所有的錨點都是藍色並加下劃線,但我們希望所有錨點都用不同的顏色,而不是加下劃線。 – Harinarayan

相關問題