Adding a header and footer with a page number in dompdf

I work with Codeigniter and I have successfully implemented dompdf to create PDF files. Now I am having problems adding the header and footer in the generated PDF file.

Here is my dompdf_helper code:

<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); function pdf_create($html, $filename='', $stream=TRUE) { require_once("dompdf/dompdf_config.inc.php"); $dompdf = new DOMPDF(); $dompdf->load_html($html); $dompdf->render(); $dompdf->set_paper("A4"); if ($stream) { $dompdf->stream($filename.".pdf",1); } else { return $dompdf->output(); } } ?> 

Here is my controller to call the PDF generation:

 <?php $data['store']=$res; $this->load->helper(array('dompdf', 'file')); $html = $this->load->view('store/sales_pdf', $data, true); $html.= $this->load->view('footer'); $filename="salesbill".$id; pdf_create($html, $filename); $data = pdf_create($html, '', false); write_file('name', $data); ?> 

I use this script to get the page number, but it is printed only if the second page comes out, otherwise it will not print.

  <script type="text/php"> if ( isset($pdf) ) { $font = Font_Metrics::get_font("helvetica", "bold"); $pdf->page_text(500,10, "Page: {PAGE_NUM} of {PAGE_COUNT}", $font, 6, array(0,0,0)); } </script> 

I want to print the company name and contact details and account number as a title on each page, and then in the footer. I want to add a page number, for example "1 of 3".

+8
codeigniter dompdf
source share
2 answers

Hope this helps you understand how to get the header and footer in dompdf. Also check out this link. Example

 <html> <head> <style> @page { margin: 180px 50px; } #header { position: fixed; left: 0px; top: -180px; right: 0px; height: 150px; background-color: orange; text-align: center; } #footer { position: fixed; left: 0px; bottom: -180px; right: 0px; height: 150px; background-color: lightblue; } #footer .page:after { content: counter(page, upper-roman); } </style> <body> <div id="header"> <h1>Widgets Express</h1> </div> <div id="footer"> <p class="page">Page <?php $PAGE_NUM ?></p> </div> <div id="content"> <p>the first page</p> <p style="page-break-before: always;">the second page</p> </div> </body> </html> 
+21
source share

I also tried adding PHP code to html, but I never had the opportunity to get it working. here is the complete inline code that I guarantee:

 require_once("dompdf_config.inc.php"); $html ='<html> <body> <p>Hello Hello</p><p style="page-break-after:always;page-break-before:always">Hello Hello 2</p><p>Hello Hello 3</p> </body> </html>'; $dompdf = new DOMPDF(); $dompdf->load_html($html); $dompdf->render(); $canvas = $dompdf->get_canvas(); $font = Font_Metrics::get_font("helvetica", "bold"); $canvas->page_text(72, 18, "Header: {PAGE_NUM} of {PAGE_COUNT}", $font, 6, array(0,0,0)); $dompdf->stream("my_pdf.pdf", array("Attachment" => 0)); ?> 
+11
source share

All Articles