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

Adding fixed header with dompdf and script tag

我正在尝试在每个带有 dompdf 分页的页面上添加一个固定的 header,只是由于某些原因无法识别这些方法:

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

我也试过在固定的 header 中添加一个像 <html><body><script type="text/php"..... 这样的字符串,但是什么也没有显示,所以这就是为什么我添加这样的代码,看看错误,错误是:

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

这很奇怪,因为我在构造函数中实例化了 class,它对方法 load_htmlset_paperrender 和 [=17= 工作正常]

DOMPDF class 没有 get_height 方法,这就是你得到这个错误的原因

但是在这个class中你可以找到:

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