DataTables PDF export with page numbers

DataTables PDF export with footer text and page numbers

by nilakshibhosale

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/2.5.0/jszip.min.js"></script>
<script src="https://cdn.rawgit.com/bpampuch/pdfmake/0.1.18/build/pdfmake.min.js"></script>
<script src="https://cdn.rawgit.com/bpampuch/pdfmake/0.1.18/build/vfs_fonts.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.15/css/jquery.dataTables.min.css">
<script src="https://cdn.datatables.net/1.10.15/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/buttons/1.3.1/js/dataTables.buttons.min.js"></script>
<link rel="stylesheet" href="https://cdn.datatables.net/buttons/1.3.1/css/buttons.dataTables.min.css">
<script src="https://cdn.datatables.net/buttons/1.3.1/js/buttons.print.min.js"></script>
<script src="https://cdn.datatables.net/buttons/1.3.1/js/buttons.html5.min.js"></script>
<table id="example">
        <thead>
            <tr>
                <th>Seq.</th>
                <th>Name</th>
                <th>Position</th>
                <th>Office</th>
                <th>Start date</th>
                <th>Salary</th>
            </tr>
        </thead>
 
        <tfoot>
            <tr>
                <th>Seq.</th>
                <th>Name</th>
                <th>Position</th>
                <th>Office</th>
                <th>Start date</th>
                <th>Salary</th>
            </tr>
        </tfoot>
 
        <tbody>
            <tr>
                <td>2</td>
                <td>Tiger Nixon</td>
                <td>System Architect</td>
                <td>Edinburgh</td>
                <td>2011/04/25</td>
                <td>$320,800</td>
            </tr>
            <tr>
                <td>22</td>
                <td>Garrett Winters</td>
                <td>Accountant</td>
                <td>Tokyo</td>
                <td>2011/07/25</td>
                <td>$170,750</td>
            </tr>
          ...

JavaScript

$('#example').DataTable( {
  dom: 'Bfrtip',
  buttons: [{
    extend: 'pdfHtml5',
    customize: function(doc) {
      doc.content[0].text = "SALES ORDER";
      doc.pageMargins = [10, 10, 45, 20];
      doc.defaultStyle.fontSize = 12;
      doc.styles.tableHeader.fontSize = 14;
      doc.styles.title.fontSize = 14;
      doc.footer = function(page, pages) {
        return {
         margin: [5,15,10,15],
          height: 30,
          columns: [{
            alignment: "left",
            margin:"10",
            text: 'This is your left footer column',
          }, {
             alignment: "right",
             text: [
               { text: page.toString(), italics: true },
                 " of ",
               { text: pages.toString(), italics: true }
             ]
          }],
          canvas: [ { type: 'line', x1: 40, y1: 0, x2: 595-40, y2: 0, lineWidth: 1 } ],
        }
      }   
    }
  }]  
});