FPDF-设置行的背景色

时间:2019-09-05 14:20:10

标签: php pdf fpdf

我需要为行设置浅灰色背景色。我为PDF使用多单元格视图。 我的代码是:

      $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++;
        }

我有一个问题,就是不完整的专栏有浅灰色背景:

enter image description here

我的行函数是:

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);
}

如何解决它并填写更正的行?

1 个答案:

答案 0 :(得分:0)

您已经计算出每行一列中的最大单元格数/高度($nb / $h)。

因此,只需在您的Rect()通话中绘制背景,而不是MulitCell()

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

在任何情况下,您都应该检查计算:您通过5 * $nb计算高度,但是MultiCell()调用中的像元高度仅为4.5。当您有更多行时,这将发生偏移。