php TCPDF如何设置两种文字颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10545405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TCPDF how to set two text color?
提问by knightrider
Currently I am trying to customize the whmcs invoice pdf. They have this following code
目前我正在尝试自定义 whmcs 发票 pdf。他们有以下代码
# 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);
?>
This code produce this format,
此代码生成此格式,
For example, if paymenet is "Unpaid", code produce this echo statement
例如,如果 paymenet 是“Unpaid”,代码会产生这个 echo 语句
UNPAID (with red color)
未付(红色)
so what I am trying to do is, I would like to add this text "Status:" infront of "UNPAID" so for example, when echo out, it will become like this
所以我想做的是,我想在“UNPAID”前面添加这个文本“Status:”,例如,当回声时,它会变成这样
"Status: UNPAID"
“状态:未付款”
I can get it by adding this code
我可以通过添加此代码来获取它
} elseif ($status=="Unpaid") {
$statustext = $_LANG["statustext"];
$statustext = $_LANG["invoicesunpaid"];
$pdf->SetTextColor(245,245,245);
But because of this code
但是因为这个代码
$pdf->SetTextColor(245,245,245);
Status: becomes (Red) as well.
状态:也变成(红色)。
What can I achieve to get Status: text black and UNPAID remained as "RED".
我能达到什么状态才能获得状态:黑色文本和未付款保持为“红色”。
Please kindly point me out. Thank you.
请指点我。谢谢你。
回答by kdobrev
I have achieved different texts with setting the text color before I need to write something with: $pdf->SetTextColor(0,0,0);
在我需要写一些东西之前,我已经通过设置文本颜色获得了不同的文本: $pdf->SetTextColor(0,0,0);
Here is an example of my code: (the actual text being written is outside of this if statement, but I change the backgroung of the text and therefore I need to change the text color to be visible )
这是我的代码示例:(正在编写的实际文本在此 if 语句之外,但我更改了文本的背景,因此我需要将文本颜色更改为可见)
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);
}
回答by k102
You can do this replacing $pdf->Cellwith $pdf->writeHTMLCellso the source will be like this:
您可以使用替换来执行此操作$pdf->Cell,$pdf->writeHTMLCell因此源将如下所示:
} elseif ($status=="Unpaid") {
$statustext = $_LANG["statustext"];
$statustext .= "<font color=red>".$_LANG["invoicesunpaid"]."</font>";
...
$pdf->writeHTMLCell(110, 20, '', '', $statustext , 1, 1, 1, true, 'J', true);
And hereis the doc for that function. Hope it helps.
而这里是这个函数的文档。希望能帮助到你。
回答by Philipp
There are 2 options for you:
有 2 个选项供您选择:
1) Output both words in separate Cells, i.e. first "Status:" then "UNPAID". Like this:
1)在单独的单元格中输出两个单词,即首先“状态:”然后是“未付款”。像这样:
$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) Use HTML markupand writeHTMLCell() to output the text:
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);

