0

I am trying to add a fixed header on every page with pagination with dompdf, just for some reasons is not recognizing the methods:

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

I also tried adding in the fixed header a string like <html><body><script type="text/php"....., but nothing shows up so that is why I added the code like this, to see the error, and error is:

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

This is very odd cause I instantieted the class in the constructor and it is working fine for methods load_html, set_paper, render and stream

hakre
  • 193,403
  • 52
  • 435
  • 836
Adrian
  • 2,273
  • 4
  • 38
  • 78

1 Answers1

1

DOMPDF class do not have get_height method that`s why you got this error

but in this class you can find:

$this->_pdf->get_height()

and $this->_pdf is not DOMPDF object but :

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

maybe try :

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
        }
szapio
  • 998
  • 1
  • 9
  • 15
  • understood, that it doesn't have the method, but how to add the header via this example? http://stackoverflow.com/questions/13159645/dompdf-page-number – Adrian Feb 02 '15 at 11:37
  • tried doing `$y = $this->_pdf->get_height() - 24`, but I get `Fatal error: Call to a member function get_height() on a non-object`, $this->_pdf understood that is not an object of DOMPDF, therefore What I need to do? Do I add your second line of code in the constructor? – Adrian Feb 02 '15 at 11:41
  • Added the second line with Canvas_factory to the constructor and get `Catchable fatal error: Argument 1 passed to Canvas_Factory::get_instance() must be an instance of DOMPDF, instance of NWIT_REPORTGEN given, called in /include/canvas_factory.cls.php on line 32` – Adrian Feb 02 '15 at 11:43
  • @Adrian, actuall i don't know this DOMPDF library, i just wrote, why it's not working. – szapio Feb 02 '15 at 11:43
  • @Adrian i updated my example according to stackoverflow.com/questions/13159645/dompdf-page-number, check if it works if not i have no idea – szapio Feb 02 '15 at 11:50
  • Doesn't work, it shows the report, but doesn't show the pagination `{PAGE_NUM}/{PAGE_COUNT}` – Adrian Feb 02 '15 at 11:55
  • @Adrian, try then `$pdf->page_text($x, $y, $PAGE_NUM.'/'.$PAGE_COUNT, $font, $size);` according to this part of docs https://code.google.com/p/dompdf/wiki/Usage#Inline_PHP_support – szapio Feb 02 '15 at 12:11
  • ok needed to add double quotes and add variables $PAGE_NUM and $PAGE_COUNT not constants. – Adrian Feb 02 '15 at 12:36