【问题标题】:Fpdf - set background color for rowFpdf - 为行设置背景颜色
【发布时间】:2019-09-05 14:20:10
【问题描述】:

我需要为行设置浅灰色背景色。我对我的 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++;
        }

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

我的行函数是:

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

我怎样才能修复它并正确填写我的行?

【问题讨论】:

  • 将标志传递给您的row 函数以指示您是否想要灰色背景。现在,您正在设置已知单元格大小之前的颜色。调整单元格大小后,在函数中设置文本和填充颜色。

标签: php pdf fpdf


【解决方案1】:

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

所以只需在您的Rect() 调用中绘制背景,而不是MulitCell()

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

无论如何,您也应该检查计算:您通过5 * $nb 计算高度,但您的MultiCell() 调用中的单元格高度仅为4.5。当您有更多行时,这将发生变化。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-08
    • 2020-12-06
    • 1970-01-01
    • 1970-01-01
    • 2011-02-11
    • 1970-01-01
    • 2019-02-28
    • 1970-01-01
    相关资源
    最近更新 更多