How to set the background color for every page tcpdf

こ雲淡風輕ζ 提交于 2019-12-05 13:26:17

if you mean apply a background color with your header function you should apply the fill color directly on the $pdf->rect function in TCPDF

Rect( $x, $y, $w, $h, $style = '', $border_style = array(), $fill_color = array() )

so in your case it would be

$this->Rect(0, 0, $this->getPageWidth(),    $this->getPageHeight(), 'DF', "",  array(220, 220, 200));

where you have to change the array in the last argument to apply your color

It don't work because on TCPDF the fill color , as far as i know, will apply on the cell and multicell function if you specified the fill argument on true

link on rect function TCPDF

foreach ($whatever as $data) {
    if($nextpage)
    {
         $this->Rect(0, 0, $this->getPageWidth(), $this->getPageHeight(), 
                   'DF', "",  array(220, 220, 200)); // where the array is the color expected
    }
}

but the better option is to define it in the header and use as the example 51 on TCPDF link to the example

public function Header() {
    // get the current page break margin
    $bMargin = $this->getBreakMargin();
    // get current auto-page-break mode
    $auto_page_break = $this->AutoPageBreak;
    // disable auto-page-break
    $this->SetAutoPageBreak(false, 0);
    // set bacground image
    $img_file = K_PATH_IMAGES.'image_demo.jpg';
    $this->Image($img_file, 0, 0, 210, 297, '', '', '', false, 300, '', false, false, 0);
    // restore auto-page-break status
    $this->SetAutoPageBreak($auto_page_break, $bMargin);
    // set the starting point for the page content
    $this->setPageMark();
}

so here it's

public function Header() {
    // get the current page break margin
    $bMargin = $this->getBreakMargin();
    // get current auto-page-break mode
    $auto_page_break = $this->AutoPageBreak;
    // disable auto-page-break
    $this->SetAutoPageBreak(false, 0);
    $this->Rect(0, 0, $this->getPageWidth(), $this->getPageHeight(), 
               'DF', "",  array(220, 220, 200)); // where the array is the color expected
    $this->SetAutoPageBreak($auto_page_break, $bMargin);
    // set the starting point for the page content
    $this->setPageMark();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!