chaitra
chaitra

Reputation: 83

How to set 2 different color for different texy in FPDF

How to set 2 different colors in FPDF??

I have tried the code below

$pdf=new FPDF();                     
$pdf->AddPage('P', 'A5');
$pdf-> SetMargins(25, 50);
$pdf->SetTextColor(91,137,42); 
$pdf->SetFont('times','',10);
$pdf -> Text (60,  37,  'Title' ); 
$pdf->SetTextColor(0,0,0);
$pdf->SetFont('times','',15);
$pdf -> Text (60,  37,  'Invoice' );

But for all tests it is displaying a single color.

Any solution for this?

Upvotes: 1

Views: 3664

Answers (1)

arkascha
arkascha

Reputation: 42885

I made a minimal working example from your code:

<?php
require('fpdf/fpdf.php');    
$pdf=new FPDF(); 
$pdf->AddPage('P', 'A5'); 
$pdf->SetMargins(25, 50); 
$pdf->SetTextColor(91,137,42); 
$pdf->SetFont('times','',10); 
$pdf->Text (60,  27,  'Title' ); 
$pdf->SetTextColor(99,0,0); 
$pdf->SetFont('times','',15); 
$pdf->Text (60,  57,  'Invoice' );    
$pdf->Output('test.pdf');

When holding the generated document open (okular test.pdf), changing the color values and re-running the code I can see the document getting updated and the colors change. The code works as expected.


Considering the first version of your question and your comments I could imagine your issue is with getting a text color in the header of a page? This also works as expected if you follow the documentation:

<?php
require('fpdf/fpdf.php');

class myPDF extends FPDF {
  function Header() {
    $this->SetFont('Arial','B',15);
    $this->setTextColor(0, 120, 120);
    $this->Cell(80);
    $this->Cell(30,10,'Page title',1,0,'C');
    $this->Ln(20);
  }
}

$pdf=new myPDF();
$pdf->AddPage('P', 'A5');
$pdf->SetMargins(25, 50);
$pdf->SetTextColor(91,137,42);
$pdf->SetFont('times','',10);
$pdf->Text (60,  27,  'Heading' );
$pdf->SetTextColor(99,0,0);
$pdf->SetFont('times','',15);
$pdf->Text (60,  57,  'Invoice' );
$pdf->Output('test.pdf');

Note that in this case the text color has to be set in the Header() function, not later, when you create a page which features the header...

This is the resulting document, you can clearly see the colors: enter image description here

Upvotes: 1

Related Questions