【问题标题】:Add background image in TCPDF在 TCPDF 中添加背景图像
【发布时间】:2022-01-13 06:24:05
【问题描述】:

在所有 tcpdf 页面中添加背景图片, 如何在 tcpdf 上添加背景图像我尝试了所有方法,但图像不会在所有其他页面中显示,只显示第一页

【问题讨论】:

    标签: php mysql pdf tcpdf


    【解决方案1】:

    请使用自定义页眉为您的 PDF 设置背景,然后每个页面都会有背景

    例如,如果你想使用'./certbg/cs.jpg'作为背景图片,那么:

     $img_file = './certbg/cs.jpg';
    

    (A) 为了正确显示背景图片,我们必须禁用自动分页符,设置背景,然后再恢复分页符 - 全部在自定义页眉中完成。

    (B) 我故意使用$pdf->AddPage(); TWICE 来演示背景图像将应用于两个页面。实际上,您可以添加更多页面,并且会看到所有页面都有背景图像。这是因为“标题”将对所有页面都有效,这是实现您想要的已知技巧。

    注意:推荐的背景图片尺寸(用于 A4 纵向 PDF):1134px * 1600px

    代码(完全工作)是:

    <?php
    
    
    require_once('./tcpdf/config/lang/eng.php');
    require_once('./tcpdf/tcpdf.php');
    
    
    // Extend the TCPDF class to create custom Header and Footer
    class MYPDF extends TCPDF {
        //Page header
        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 = './certbg/cs.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();
        }
    }
    
    // create new PDF document
    $pdf = new MYPDF("P", PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
    
    // set document information
    $pdf->SetCreator(PDF_CREATOR);
    $pdf->SetAuthor('Author Name');
    $pdf->SetTitle('PDF');
    $pdf->SetSubject('PDF');
    $pdf->SetKeywords('TCPDF, HK');
    
    //$pdf->SetProtection(array('print', 'copy'), '', null, 0, null);
    
    $pdf->SetProtection(array('copy'), '', null, 0, null);
    
    
    // set header and footer fonts
    $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
    
    // set default monospaced font
    $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
    
    // set margins
    $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
    $pdf->SetHeaderMargin(0);
    $pdf->SetFooterMargin(0);
    
    // remove default footer
    $pdf->setPrintFooter(false);
    
    // set auto page breaks
    $pdf->SetAutoPageBreak(TRUE, 0);
    
    // set image scale factor
    $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
    
    // set some language-dependent strings (optional)
    if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
        require_once(dirname(__FILE__).'/lang/eng.php');
        $pdf->setLanguageArray($l);
    }
    
    // ---------------------------------------------------------
    
    // set font
    $pdf->SetFont('times', '', 48);
    
    // add a page
    $pdf->AddPage();
    
    $html.= '<table border=0 style="">
    <tr><td style="height:180px;">&nbsp;</td></tr>
    
    <tr><td align="center" style="font-size:80px;height:180px;color:#fa8832;text-align:center;">'. ' Page 1'. '</td></tr>
    <tr><td style="height:450px;">&nbsp;</td></tr>
    
    <tr><td  align="center"  style="font-size:30px;color:#444444;">'.   ' Page 1 '  . '</td></tr>
    </table>';
    
    
    $pdf->writeHTML($html, true, false, true, false, '');
    
    
    $html= '<table border=0 style="">
    <tr><td style="height:180px;">&nbsp;</td></tr>
    
    <tr><td align="center" style="font-size:80px;height:180px;color:#fa8832;text-align:center;">'. ' Page 2'. '</td></tr>
    <tr><td style="height:450px;">&nbsp;</td></tr>
    
    <tr><td  align="center"  style="font-size:30px;color:#444444;">'.   ' Page 2 '  . '</td></tr>
    </table>';
    
    
    $pdf->AddPage();
    
    $pdf->writeHTML($html, true, false, true, false, '');
    
    
    // ---------------------------------------------------------
    
    $y1=chr(mt_rand(65, 90));
    $y2=chr(mt_rand(65, 90));
    $y3=chr(mt_rand(48, 57));
    $y4=chr(mt_rand(65, 90));
    $y5=chr(mt_rand(48, 57));
    $y6=chr(mt_rand(65, 90));
    $y7=chr(mt_rand(48, 57));
    $y8=chr(mt_rand(48, 57));
    $y9=chr(mt_rand(65, 90));
    
    $ry=$y1.$y2.$y3.$y4.$y5.$y6.$y7.$y8.$y9;
    
    
     $pdf->Output($ry.".pdf",'FD');
    
    ?>
    
    

    【讨论】:

      猜你喜欢
      • 2011-02-25
      • 2018-06-26
      • 2016-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-11
      相关资源
      最近更新 更多