Php 如何设置每页tcpdf的背景色

Php 如何设置每页tcpdf的背景色,php,html,css,pdf-generation,tcpdf,Php,Html,Css,Pdf Generation,Tcpdf,我目前正在使用TCPDF在我的web应用程序中生成简历。但由于对css的支持有限,我陷入了困境。 现在,我尝试为生成的每个页面应用背景色。但我只知道第一页的颜色 我的代码是: <?php class PROFILE_PDF extends TCPDF { public function Header() { $this->SetFillColor(52, 21, 0, 76); $this->Rect(0, 0, $this-&g

我目前正在使用TCPDF在我的web应用程序中生成简历。但由于对css的支持有限,我陷入了困境。 现在,我尝试为生成的每个页面应用背景色。但我只知道第一页的颜色

我的代码是:

<?php
class PROFILE_PDF extends TCPDF
{
    public function Header()
    {
        $this->SetFillColor(52, 21, 0, 76);
        $this->Rect(0, 0, $this->getPageWidth(), $this->getPageHeight(), 'DF', "");
    }


    private $footer_data = array();


    public function Footer()
    {
        // Position at 15 mm from bottom
        $this->SetY(-15);
        // Set font
        $this->SetFont('helvetica', 'I', 8);
        // Page number


        $name = <<< EOD
        <p>Curriculum Vitae - {$this->footer_data["name"]}</p>
        <style>
            p {
                color: #F5F5F5;
            }
        </style>

        EOD;
        $this->writeHTMLCell(0, 10, '', '', $name, 0, 1, 0, true, 'L', true);

        $page = <<< EOD

        <p>Page  {$this->getAliasNumPage()} /   {$this->getAliasNbPages()}</p>
        <style>
            p {
                color: #F5F5F5;
            }
        </style>

        EOD;

        $this->writeHTMLCell(0, 10, '', 284, $page, 0, 1, 0, true, 'R', true);

        $style = array('width' => 0.5, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(128, 229, 255));
        $this->Line(0, 275, 250, 275, $style);
    }

    public function setFooterData($footer_data)
    {
        $this->footer_data = $footer_data;
    }

$pdf = new PROFILE_PDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);


$pdf->SetLineStyle(array('width' => 2, 'color' => array(112, 128, 144)));

$pdf->Line(0, 0, $pdf->getPageWidth(), 0);
$pdf->Line($pdf->getPageWidth(), 0, $pdf->getPageWidth(), $pdf->getPageHeight());
$pdf->Line(0, $pdf->getPageHeight(), $pdf->getPageWidth(), $pdf->getPageHeight());
$pdf->Line(0, 0, 0, $pdf->getPageHeight());

// set header data
$footer_data = array();
$footer_data["name"] = $personal_data["first_name"] . " " . $personal_data["last_name"];
$pdf->setFooterData($footer_data);

$pdf->setPrintHeader(false);
//$pdf->Header();
// set document information
$pdf->SetCreator(getSiteName());
$pdf->SetAuthor('Mobile Solutions');
$pdf->SetTitle($personal_data["first_name"] . " " . $personal_data["last_name"]);
$pdf->SetSubject("Student's profile");
$pdf->setPrintHeader(false);

$pdf->AddPage();

$pdf->SetFillColor(52, 21, 0, 76);
$pdf->Rect(0, 0, $pdf->getPageWidth(), $pdf->getPageHeight(), 'DF', "");


$pdf->SetFont('courier', 'B', 24);

//some html elements

$pdf->Output($personal_data["first_name"] . "_" . $personal_data["last_name"] . ".pdf", 'D');
?>


在这里,我无法将
SetFillColor()
函数应用于下一个生成的页面。原因是什么?TCPDF文档说明它应该应用于所有页面。

如果您的意思是在
标题
函数中应用背景色,则应直接在TCPDF中的
$pdf->rect
函数上应用填充色

Rect( $x, $y, $w, $h, $style = '', $border_style = array(), $fill_color = array() )
所以在你的情况下是这样的

$this->Rect(0, 0, $this->getPageWidth(),    $this->getPageHeight(), 'DF', "",  array(220, 220, 200));
您必须更改最后一个参数中的数组才能应用颜色

它不起作用,因为据我所知,在TCPDF上,如果将fill参数指定为true,则填充颜色将应用于
单元格
多单元格
函数

但更好的选择是在标题中定义它,并将其用作TCPDF上的示例51

就在这里

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

你尝试在什么上应用你的fillColor?我在标题中应用了SetFillColor,以便应用于所有页面。是否应该更改?这次我尝试获取页码,如果页码大于1,则设置背景色。但它也不起作用。如果($pdf->getAliasNumPage()>1){$this->SetFillColor(52,21,0,76);$this->Rect(0,0,$this->getPageWidth(),$this->getPageHeight(),'DF','';}感谢您的回复。那么,如何将颜色应用于页面,而不是每个单元格?请参考此问题。我发现了问题所在。我错放了:$pdf->SetAutoPageBreak(TRUE,pdf\u MARGIN\u BOTTOM);
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();
}
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();
}