7

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".

halfer
  • 19,824
  • 17
  • 99
  • 186
user936565
  • 175
  • 2
  • 2
  • 13
  • 1
    The first place to start when debugging dompdf is to look at the HTML document you're rendering, rather than the process that creates the document. It's easier to identify errors related to dompdf that way. So, can you post a sample document that exhibits the error? My only question would be, do you at least have body tags around the HTML content? If not then the script will be inaccessible to dompdf due to how the document processes. – BrianS Oct 09 '12 at 02:32

3 Answers3

22

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>
Venkata Krishna
  • 4,287
  • 6
  • 30
  • 53
  • 2
    I'm also trying to add a page number but this didn't work for me. I tried 'Page of ' and '' and '' and none of them output anything. I do have php enabled in my options. Any thoughts? – danielson317 Aug 05 '16 at 15:37
  • Same here, same try and errors. Can't get it to work. Only way is CSS' `counter(page)`, but can't get `counter(pages)` to show something else than `0`... – Philipp Gächter Dec 21 '16 at 11:43
12

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));
?>
user2087697
  • 121
  • 1
  • 3
0
<style>
        /** 
            Set the margins of the page to 0, so the footer and the header
            can be of the full height and width !
         **/
        @page {
            margin: 0cm 0cm;
        }

        /** Define now the real margins of every page in the PDF **/
        body {
            margin-top: 4cm;
            margin-left: 2cm;
            margin-right: 2cm;
            margin-bottom: 4cm;
        }

        /** Define the header rules **/
        header {
            position: fixed;
            top: 2cm;
            left: 2cm;
            right: 2cm;
            height: 2cm;

            /** Extra personal styles 
            background-color: #03a9f4;
            color: white;
            **/
            text-align: center;
            line-height: 1.5cm;
        }

        /** Define the footer rules **/
        footer {
            position: fixed; 
            bottom: 0cm; 
            left: 0cm; 
            right: 0cm;
            height: 2cm;

            /** Extra personal styles **/
            background-color: #03a9f4;
            color: white;
            text-align: center;
            line-height: 1.5cm;
        }
    </style>
    <header>
        <table  class="table table-bordered" border='1' width='100%' style='font-size:16px; border-collapse: collapse;/* border-collapse: separate; border-spacing: 30px;*/'>
            <tr> 
                <th scope="col" style="padding:10px;" width="10%">Clause No.</th> 
                <th scope="col" style="padding:10px;" width="80%">INSTRUCTIONS TO BIDDERS</th> 
            </tr>
        </table>
    </header>
    <?php endif;?>
    <footer>
        Copyright &copy; <?php echo date("Y");?> 
    </footer>

Use header footer tag