使用dompdf和脚本标记添加固定标头

时间:2015-02-02 11:31:25

标签: php oop dompdf

我正在尝试在每个页面上添加固定标题,并使用dompdf分页,只是出于某些原因无法识别方法:

require("NWIT.dompdf/dompdf_config.inc.php");
class NWIT_REPORTGEN extends NWIT_RAMS{

    protected $pdfdom;
    function __construct() {
        $this->pdfdom = new DOMPDF();
    }

    function generate_report() {
        $name_of_generated_report = 'hello';
        $font = Font_Metrics::get_font("helvetica", "normal");
        $size = 9;
        $y = $this->pdfdom->get_height() - 24;
        $x = $this->pdfdom->get_width() - 15 - Font_Metrics::get_text_width("1/1", $font, $size);
        $fixed_header = $this->pdfdom->page_text($x, $y, "{PAGE_NUM}/{PAGE_COUNT}", $font, $size);
        $some_html = '<html></html><br><br>hellor</br><body></html>';
        $content = $fixed_header.$some_html;
        $this->pdfdom->load_html($content);
        $this->pdfdom->set_paper('a4', 'landscape');
        $this->pdfdom->render();
        $this->pdfdom->stream(array($name_of_generated_report, 0, 1, 0)); //for testing
    }
}

我还尝试在固定标头中添加一个像<html><body><script type="text/php".....这样的字符串,但没有显示任何内容,这就是为什么我添加了这样的代码,以查看错误,错误是:

Fatal error: Call to undefined method DOMPDF::get_height() in line 13

这很奇怪,因为我在构造函数中对类进行了快速操作,并且它适用于方法load_htmlset_paperrenderstream

1 个答案:

答案 0 :(得分:1)

DOMPDF类没有get_height方法,这就是你遇到此错误的原因

但在本课程中你可以找到:

$this->_pdf->get_height()

$this->_pdf不是DOMPDF对象,但是:

 $this->_pdf = Canvas_Factory::get_instance($this, $this->_paper_size, $this->_paper_orientation);

也许试试:

function generate_report() {
        $name_of_generated_report = 'hello';
        $font = Font_Metrics::get_font("helvetica", "normal");
        $size = 9;
        $fixedheader = "<script type='text/php'>
  if ( isset($pdf) ) { 
    $font = Font_Metrics::get_font('helvetica', 'normal');
    $size = 9;
    $y = $pdf->get_height() - 24;
    $x = $pdf->get_width() - 15 - Font_Metrics::get_text_width('1/1', $font, $size);
    $pdf->page_text($x, $y, $PAGE_NUM.'/'.$PAGE_COUNT, $font, $size);
  } 
</script>";
        $some_html = '<html></html><br><br>hellor</br><body></html>';
        $content = $fixed_header.$some_html;
        $this->pdfdom->load_html($content);
        $this->pdfdom->set_paper('a4', 'landscape');
        $this->pdfdom->render();
            $this->pdfdom->stream(array($name_of_generated_report, 0, 1, 0)); //for testing
        }