dou760663 2017-09-18 12:18 采纳率: 100%
浏览 551
已采纳

如何为每个页面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('hSenid 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文档说它应该适用于所有页面。

  • 写回答

1条回答 默认 最新

  • dongyi7966 2017-09-18 12:33
    关注

    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();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面