Reputation: 191
I want it so that the text saying white will use SetTextColor as white, and the orange to use orange.
$pdf->SetTextColor(255,255,255);
$pdf->Cell(50,0,'WHITE ORANGE ORANGE WHITE',0,1,'C');
How do I affect the 'ORANGE' words to use an orange text color?
Upvotes: 10
Views: 78416
Reputation: 1
You should replace this:
$pdf->Cell(50,0,'WHITE ORANGE ORANGE WHITE',0,1,'C');
for this
$pdf->Cell(50,0,'WHITE ORANGE ORANGE WHITE',0,1,'C', true);
//Add the true param at the end
Upvotes: -1
Reputation: 135
You may also use a writeHTML
method (tcpdf ver 6.2) like so
$html = 'Simple text <span style="color: rgb(255,66,14);">Orange</span> simple <span style="color: rgb(12,128,128);">Turquoise</span>';
$this->writeHTML($html, true, false, true, false, '');
Upvotes: 0
Reputation: 346
IF you don't need to use the Cell method, you could use the Write method instead:
$pdf->SetFont('Arial','b',12);
$pdf->SetTextColor(153,0,153);
$pdf->Write(7,'Text in color, ');
$pdf->SetFont('Arial','',12);
$pdf->SetTextColor(0,0,0);
$pdf->Write(7,'and text in black all in the same line'));
$pdf->Ln(7);
Upvotes: 3
Reputation: 13
I had to do a similar thing. Instead of color I had to change the size of the font. in my cell I called the function instead so,in your case you can do this
$pdf->Cell(50,0,white($pdf,'White').orange($pdf,'orange'),0,1,'C');
and define the function as
function white($pdf,$val){
$pdf->SetTextColor(255,255,255);
return $pdf->Text(0,0,$val);
}
and same goes for orange.
TIP: to position it properly use getX() and getY()
Upvotes: 1
Reputation: 318
I needed that functionality too. This is the function I written to do a simple colored string:
function cellMultiColor($stringParts) {
$currentPointerPosition = 0;
foreach ($stringParts as $part) {
// Set the pointer to the end of the previous string part
$this->_pdf->SetX($currentPointerPosition);
// Get the color from the string part
$this->_pdf->SetTextColor($part['color'][0], $part['color'][1], $part['color'][2]);
$this->_pdf->Cell($this->_pdf->GetStringWidth($part['text']), 10, $part['text']);
// Update the pointer to the end of the current string part
$currentPointerPosition += $this->_pdf->GetStringWidth($part['text']);
}
and you use it like this:
cellMultiColor([
[
'text' => 'Colored string example: ',
'color' => [0, 0, 0],
],
[
'text' => 'red',
'color' => [255, 0, 0],
],
[
'text' => ', ',
'color' => [0, 0, 0],
],
[
'text' => 'blue',
'color' => [0, 0, 255],
],
]);
Upvotes: 5
Reputation: 321
It is possible with a little trick. I just made it printing 2 Cells, one over the other, like this:
//Setting the text color to black
$pdf->SetTextColor(0,0,0);
//Printing my cell
$pdf->SetFont('Arial','B');
$pdf->Cell(55,5,"Black Text ",1,0,'C');
$pdf->SetXY($coordXbase,$coordY);
//Setting the text color to red
$pdf->SetTextColor(194,8,8);
//Printing another cell, over the other
$pdf->SetFont('Arial','B');
//Give some space from the left border, and print the red text after the black text that is in the cell behind this one.
$pdf->Cell(55,5," Red Text",0,0,'C');
$pdf->SetXY($coordXbase,$coordY);
//Setting the text color back to back, in the next cells.
$pdf->SetTextColor(0,0,0);
The result was this:
As I was a little rush, I had no time to create some function to help with this, but this would be a good starting point idea :)
P.S.: Tell if you guys find an easier way.
Upvotes: 8
Reputation: 3740
Answer #1: You can't. A cell by definition is uniform in font and color. You can measure the width of the words with getStringWidth and do it in a series of Cells.
Answer #2: Many of the contributed scripts are based on constructing variants of built-in functions. After all, you have the PHP code right there for all of FPDF. You can make your own Cell_plus function that takes an array of phrases and another array or two or three of attributes. Then maybe contribute it as an additional script.
Upvotes: 0