简体   繁体   中英

Fpdf - set background color for row

I need set lightgrey background color for row. I use multicell view for my PDF. My code is:

      $countRow = 0;
        foreach ($arrPeriod as $key=>$val) {
            if($countRow % 2 == 0){
                $this->setFillColor(230,230,230);
                $this->SetTextColor(0,0,0);
            }else{
                $this->setFillColor(255,255,255);
                $this->SetTextColor(0,0,0);
            }
            $this->Row([
                     $val['lead_name'],
                     $val['content'],
                     $val['date_due']
                ]
            );
            $countRow++;
        }

I have problem that not full column has lightgrey background:

在此处输入图片说明

My Row function is:

function Row($data)
{
    //Calculate the height of the row
    $nb = 0;
    for ($i = 0; $i < count($data); $i++) {
        $nb = max($nb,$this->GetMultiCellHeight($this->widths[$i], $data[$i]));
    }
    $h = 5 * $nb;
    //Issue a page break first if needed
    $this->CheckPageBreak($h);
    //Draw the cells of the row
    for ($i = 0; $i < count($data); $i++) {
        $w = $this->widths[$i];
        $a = isset($this->aligns[$i]) ? $this->aligns[$i] : 'L';
        //Save the current position
        $x = $this->GetX();
        $y = $this->GetY();
        //Draw the border
        $this->Rect($x, $y, $w, $h);
        //Set font
        if ($i == 0 || $i == 2) {
            $this->SetFont('Arial', 'B', 10);
        } else {
            $this->SetFont('Arial', '', 10);
        }
        //Print the text
        $this->MultiCell($w, 4.5, $data[$i], 0, $a, true);
        //Put the position to the right of the cell
        $this->SetXY($x + $w, $y);
    }
    //Go to the next line
    $this->Ln($h);
}

How can I fix it and fill correct my row?

You already calculate the maximum number of cells/height in a column per row ( $nb / $h ).

So just draw the background in your Rect() call instead of the MulitCell() :

$this->Rect($x, $y, $w, $h, true);

In any case you should check the caluclation, too: You calculate the height by 5 * $nb but the cell height in your MultiCell() call is only 4.5 . This will shift of when you have more lines.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM