Background Color inside a cell is not changing in fpdf
Asked Answered
C

3

6

Im using fpdf to generate a pdf file for my reports and Im new to this. I want to change the fill color inside a cell but whenever I reloaded the page, nothing hapeens, It still the same white fill color. Here is my code :

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

   $pdf = new FPDF('P','pt','Letter');
   $pdf->SetFillColor(230,230,230);

   $pdf->SetTitle("Title Here");    
   $pdf -> AddPage();
   $pdf -> SetFont('Arial','',12); 

?>

What is wrong with my code? I followed the proper way of setting the fill color but nothing happens? Can anyone help me fix it? Thanks

Cerellia answered 12/1, 2015 at 3:26 Comment(0)
N
22

Tyr something like:

$pdf->setFillColor(230,230,230); 
$pdf->Cell(0,10,$text,0,1,'L',1); //your cell

Defines the color used for all filling operations (filled rectangles and cell backgrounds). It can be expressed in RGB components or gray scale. The method can be called before the first page is created and the value is retained from page to page.

Take a look to the FPDF manual

Noshow answered 12/1, 2015 at 3:55 Comment(2)
Good answer @JuanSedano. It was helpful to understand that using a 1 is true and 0 is false option in Cell is how you can fill the background. Thank you for the answer.Quint
Important to have the above statements in the same order, that is 1st setFillColor and then Cell params. Just wasted a few hours looking at code that was the other way around (Cell first and then setFillColor) and wasn't doing exactly what I wanted.Elwira
R
10
mpdf->SetFillColor('RED');

Just set the seventh parameter equal to TRUE

$mpdf->WriteCell(38, 10, 'HELLO', 1, 0, 'C', TRUE);
Resonator answered 25/3, 2016 at 9:51 Comment(0)
U
0

This solution worked for me:

$pdf->setFillColor(230,230,230);
$pdf->MultiCell(0, 6, $text, 0, '', 1)

Misteriously the setFillColor() doesn't looks to work properly with $pdf->Cell(), but just replacing it with $pdf->MultiCell() does the job.

I don't know how, but it works. You just need to play a bit with the 2nd parameter ($h) to set the height of the cell, for me 6 works exactly as I want. Otherwise it works completely fine.

Unroot answered 22/7 at 15:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.