knightrider
knightrider

Reputation: 2583

TCPDF how to set two text color?

Currently I am trying to customize the whmcs invoice pdf. They have this following code

    # 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

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

"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.

Upvotes: 4

Views: 41389

Answers (3)

Philipp
Philipp

Reputation: 11401

There are 2 options for you:

1) Output both words in separate Cells, i.e. first "Status:" then "UNPAID". Like this:

$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 markup and writeHTMLCell() to output the text:

$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); 

Upvotes: 0

k102
k102

Reputation: 8089

You can do this replacing $pdf->Cell with $pdf->writeHTMLCell so the source will be like this:

} 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 here is the doc for that function. Hope it helps.

Upvotes: 2

kdobrev
kdobrev

Reputation: 312

I have achieved different texts with setting the text color before I need to write something with: $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 ($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);
    }

Upvotes: 9

Related Questions