2012-05-11 167 views
3

目前我正在嘗試自定義whmcs發票pdf。他們有這個下面的代碼TCPDF如何設置兩種文字顏色?

# Payment Status 
$endpage = $pdf->GetPage(); 
$pdf->setPage($startpage); 
$pdf->SetXY(85,$addressypos); 
if ($status=="Cancelled") { 
    $statustext = $_LANG["invoicescancelled"]; 
    $pdf->SetTextColor(245,245,245); 
} elseif ($status=="Unpaid") { 
    $statustext = $_LANG["invoicesunpaid"]; 
    $pdf->SetTextColor(204,0,0); 
} elseif ($status=="Paid") { 
    $statustext = $_LANG["invoicespaid"]; 
    $pdf->SetTextColor(153,204,0); 
} elseif ($status=="Refunded") { 
    $statustext = $_LANG["invoicesrefunded"]; 
    $pdf->SetTextColor(34,68,136); 
} elseif ($status=="Collections") { 
    $statustext = $_LANG["invoicescollections"]; 
    $pdf->SetTextColor(255,204,0); 
} 
$pdf->SetFont('freesans','B',12); 
$pdf->Cell(110,20,strtoupper($statustext),0,0,'C'); 
$pdf->setPage($endpage); 

?> 

此代碼產生這種格式,

例如,如果paymenet是 「無薪」,代碼產生這種回聲聲明

UNPAID(用紅色)

所以我想要做的是,我想添加這個文本「狀態:」infront的「UNPAID」,例如,當回聲出來時,它會變成這樣的

「狀態:未付」

我可以通過添加該代碼

} elseif ($status=="Unpaid") { 
     $statustext = $_LANG["statustext"]; 
    $statustext = $_LANG["invoicesunpaid"]; 
    $pdf->SetTextColor(245,245,245); 

得到它,但因爲這個代碼

$pdf->SetTextColor(245,245,245); 

狀態:變(紅)以及。

我能做些什麼來獲得狀態:黑色文字和UNPAID保持爲「紅色」。

請親切指出我。謝謝。

回答

2

爲此,您可以用$pdf->writeHTMLCell所以源更換$pdf->Cell將是這樣的:

} elseif ($status=="Unpaid") { 
    $statustext = $_LANG["statustext"]; 
    $statustext .= "<font color=red>".$_LANG["invoicesunpaid"]."</font>"; 
... 
$pdf->writeHTMLCell(110, 20, '', '', $statustext , 1, 1, 1, true, 'J', true); 

而且here是這個函數的文檔。希望能幫助到你。

3

我已經實現了不同的文字設置文本的顏色,然後我需要用下面的代碼寫下:$ pdf-> SetTextColor(0,0,0);

這裏是我的代碼示例: (被寫入的實際文本if語句的這外面,但我改變文本的backgroung,因此我需要改變文字顏色是可見的)

if ($row_cnt > ($sticker_count/2)) { 
     $pdf->SetTextColor(0,0,0); 
     $pdf->ImageSVG($file='images/sticker_29072013.svg', $xsi, $ysi, $w='60', $h='30', $link='', $align='', $palign='', $border, $fitonpage=false); 
     $pdf->ImageSVG($file='images/qr_29072013.svg', $xqi, $yqi, $w='30', $h='30', $link='', $align='', $palign='', $border, $fitonpage=false); 
    } 
    else { 
     $pdf->SetTextColor(255,255,255); 
     $pdf->ImageSVG($file='images/blacksticker_29072013.svg', $xsi, $ysi, $w='60', $h='30', $link='', $align='', $palign='', $border, $fitonpage=false); 
     $pdf->ImageSVG($file='images/blackqr_29072013.svg', $xqi, $yqi, $w='30', $h='30', $link='', $align='', $palign='', $border, $fitonpage=false); 
    } 
0

有你2個選擇:在單獨的細胞

1)輸出這兩個詞,即第一 「狀態:」,然後 「UNPAID」。就像這樣:

$endpage = $pdf->GetPage(); 
$pdf->setPage($startpage); 
$pdf->SetXY(85,$addressypos); 
$pdf->SetFont('freesans','B',12); 

// First output "Status:" in black color. 
$pdf->setTextColor(array(0,0,0)); 
$pdf->Cell(110,20,'Status:',0,0,'L'); // Not centered anymore! 

// Now run your existing condition to get the status text/color: 
if ($status=="Cancelled") { 
    $statustext = $_LANG["invoicescancelled"]; 
    $pdf->SetTextColor(245,245,245); 
} elseif ($status=="Unpaid") { 
    $statustext = $_LANG["invoicesunpaid"]; 
    $pdf->SetTextColor(204,0,0); 
} elseif ($status=="Paid") { 
    $statustext = $_LANG["invoicespaid"]; 
    $pdf->SetTextColor(153,204,0); 
} elseif ($status=="Refunded") { 
    $statustext = $_LANG["invoicesrefunded"]; 
    $pdf->SetTextColor(34,68,136); 
} elseif ($status=="Collections") { 
    $statustext = $_LANG["invoicescollections"]; 
    $pdf->SetTextColor(255,204,0); 
} 

// Output the status text at X = 125 (1.5cm further to the right!) 
// Note that the word "Status" above used X = 110. 
$pdf->Cell(125,20,strtoupper($statustext),0,0,'L'); // Not centered anymore! 
$pdf->setPage($endpage); 

2)使用HTML標記和writeHTMLCell()來輸出的文本:

$endpage = $pdf->GetPage(); 
$pdf->setPage($startpage); 
$pdf->SetXY(85,$addressypos); 
$pdf->SetFont('freesans','B',12); 

// Now run your existing condition to get the status text/color: 
if ($status=="Cancelled") { 
    $statustext = $_LANG["invoicescancelled"]; 
    $color = 'rgb(245,245,245)'; 
} elseif ($status=="Unpaid") { 
    $statustext = $_LANG["invoicesunpaid"]; 
    $color = 'rgb(204,0,0)'; 
} elseif ($status=="Paid") { 
    $statustext = $_LANG["invoicespaid"]; 
    $color = 'rgb(153,204,0)'; 
} elseif ($status=="Refunded") { 
    $statustext = $_LANG["invoicesrefunded"]; 
    $color = 'rgb(34,68,136)'; 
} elseif ($status=="Collections") { 
    $statustext = $_LANG["invoicescollections"]; 
    $color = 'rgb(255,204,0)'; 
} 

// Now build an HTML string for the status: 
$html = '<font color="black">Status:</font> '. 
    '<font style="color:$color">$statustext</font>'; 

// Output the HTML code 
$pdf->writeHTMLCell(110,20,0,0,$html); 
$pdf->setPage($endpage);