Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FPDF: Change text color while inside a Cell?

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?

like image 726
Michael Crothers Avatar asked Jan 14 '13 23:01

Michael Crothers


People also ask

How do I change the text color in FPDF?

$pdf->SetTextColor(255,255,255); $pdf->Cell(50,0,'WHITE ORANGE ORANGE WHITE',0,1,'C');

How do you use MultiCell in FPDF?

MultiCell( width of cell, height of each line, text content, border, alignment of the text, fill boolean). An example would be : $this ->MultiCell(25,6,”Here's some text for display”, 'LRT', 'L', 0); In the above example, the MultiCell width is set at 25 units that were specified in the initial call to FPDF.

How do you wrap text in FPDF?

$w_w=$c_height/3; example:if you want to wrap a word with 3 line. and the cell height is 9 so 9/3=3. first w_w line will be echo in height 3 and next line is echoed in height 6 and next line will be in height 9.

How do you use FPDF?

The PDFlib needs to be installed as an extension in your PHP package, whereas FPDF can just be included in your PHP script and it's ready to use. To get started, you will need to download the FPDF class from the FPDF Web site and include it in your PHP script like this: require('fpdf.


2 Answers

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:

Multicollor cell result

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.

like image 197
Eder Franco Avatar answered Oct 17 '22 00:10

Eder Franco


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],
    ],
]);
like image 44
Ioulian Alexeev Avatar answered Oct 16 '22 22:10

Ioulian Alexeev