Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding header and footer with page number in dompdf

I am working with Codeigniter and I successfully implemented dompdf for generating PDF files. Now I have issues on adding a header and footer in generated PDF.

Here is the 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 the my controller to call 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 for getting page number but it printed only if second page is exits otherwise it won't 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 bill number as header in every page then in a footer. I want to add a page number like "1 of 3".

like image 208
user936565 Avatar asked Oct 08 '12 06:10

user936565


People also ask

How do I add a header and footer in Dompdf?

// your dompdf setup $dompdf = new DOMPDF(); $dompdf->load_html($html); $dompdf->render(); // add the header $canvas = $dompdf->get_canvas(); $font = Font_Metrics::get_font("helvetica", "bold"); // the same call as in my previous example $canvas->page_text(72, 18, "Header: {PAGE_NUM} of {PAGE_COUNT}", $font, 6, array(0 ...

How do you do a page break in Dompdf?

Using page-break-inside: auto; basically says to dompdf "do what you would normally do when breaking pages." To force a page break before/after your table you would use page-break-before: always; / page-break-after: always; . To ask dompdf to avoid breaking inside an element you would use page-break-inside: avoid; .

How do I display an image in Dompdf?

The best solution I found for displaying images is to convert the image into Base64 and display them as base64 when the PDF is generated. The below code will explain about how to do it.. $dompdf = new Dompdf( $options ); $dompdf ->loadHTML( $HTML );

What is Dompdf library?

Dompdf is an HTML to PDF converter. At its heart, dompdf is (mostly) a CSS 2.1 compliant HTML layout and rendering engine written in PHP. It is a style-driven renderer: it will download and read external stylesheets, inline style tags, and the style attributes of individual HTML elements.


2 Answers

I hope this will help you lot in understanding how to get header and footer in dompdf. Check this link also....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>
like image 62
Venkata Krishna Avatar answered Oct 04 '22 06:10

Venkata Krishna


I have also tried to add PHP code into the html but have never got a chance to make it work. here is the complete inline code which I guarantee works:

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));
?>
like image 28
user2087697 Avatar answered Oct 04 '22 06:10

user2087697