php - 如何为每个页面设置背景颜色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->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 文档说它应该适用于所有页面。

最佳答案

如果你的意思是用你的 header 函数应用背景颜色,你应该直接在 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 上指定了填充参数,填充颜色将应用于 cellmulticell 函数是的

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

但更好的选择是在标题中定义它并用作 TCPDF 上的示例 51 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();
}

原来是这样

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

关于php - 如何为每个页面设置背景颜色tcpdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46279119/

相关文章:

asp.net - ASP :ListItem store object

javascript - 如何使用 Bootstrap 模式通过 jQuery 将字段添加到表单

css - 在 django-autocomplete-light 3.1.3 上修改 CSS

html - 我的 Bootstrap 页面页脚下方的空白

css - Bootstrap 4 : Equal width columns using grid

php - file_get_contents 在读取文件时显示意外输出

php - 在帐单地址后的Woocommerce订单接收页面中添加文本

php - 可以在 SELECT 查询中使用 IF() 但不能使用 UPDATE

php - 不要显示值(value) 0 美元的 WooCommerce 订阅的经常性价格

jquery - 添加图像时 Bootstrap Carousel 导航消失