How can I make a custom dynamic footer in TCPDF with data taken from a database?

元气小坏坏 提交于 2019-12-11 08:38:03

问题


I would like to make a dynamic footer containing data taken from a database. How to extend TCPDF class to put those data in?

// my DB stuff here
$datafromdb = getDataFromDB();

 class MYPDF extends TCPDF {
    // Page footer
    public function Footer() {
        // Position at 10 mm from bottom
        $this->SetY(-10);
        // Set font
        $this->SetFont('dejavusans', 'I', 8);
        $foot = $datafromdb.'Page '.$this->getAliasNumPage().'/'.$this->getAliasNbPages();

        $this->MultiCell(0, 10, $foot, 0, 'C');
    }
}

回答1:


you can add a __construct method to pass your data.
try this :

// my DB stuff here
$datafromdb = getDataFromDB();

class MYPDF extends TCPDF {
    private $datafromdb ;//<-- to save your data

    function __construct( $datafromdb , $orientation, $unit, $format ) 
    {
        parent::__construct( $orientation, $unit, $format, true, 'UTF-8', false );

        $this->datafromdb = $datafromdb ;
        //...
    }
    // Page footer
    public function Footer() {
        // Position at 10 mm from bottom
        $this->SetY(-10);
        // Set font
        $this->SetFont('dejavusans', 'I', 8);
        $foot = $this->datafromdb.'Page '.$this->getAliasNumPage().'/'.$this->getAliasNbPages();

        $this->MultiCell(0, 10, $foot, 0, 'C');
    }
}


来源:https://stackoverflow.com/questions/20474796/how-can-i-make-a-custom-dynamic-footer-in-tcpdf-with-data-taken-from-a-database

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!