Search code examples
javascriptjquerydatatable

page number not working in datatable export to pdf


I am trying to add page number in datatble pdf export, I got code from its official site after adding this code pdf button got disappearing and getting 'Uncaught ReferenceError: doc is not defined at HTMLDocument' error in console.

$('#table2').DataTable({
    
    dom: 'Bfrtip',
   
    buttons: [
         
        {
            extend: 'pdfHtml5',
            title: 'Data export',
            filename: 'dt_custom_pdf',
            pageSize: 'A4',
                exportOptions: {
                    columns: ':visible',
                    search: 'applied',
                    order: 'applied'
                },
           
            
        },
       doc['footer']=(function(page, pages) {
        return {
        columns: [
        'Left part of footer',
            {
                alignment: 'right',
                text: [
                    { text: page.toString(), italics: true },
                    ' of ',
                    { text: pages.toString(), italics: true }
                ]
            }
        ],
        margin: [10, 0]
        }
        });
    ]
   });

Solution

  • You did not define customize function to dataTable that why getting doc error you need to change

    $('#table2').DataTable({
        
        dom: 'Bfrtip',
       
        buttons: [
             
            
            {
            extend: 'pdfHtml5',
            text: 'Pdf',
            filename: 'dt_custom_pdf',
            pageSize: 'A4',
                exportOptions: {
                    columns: ':visible',
                    search: 'applied',
                    order: 'applied'
                },
            customize: function ( doc ) {
                doc['footer']=(function(page, pages) {
                    return {
                    columns: [
                    
                    {
                    alignment: 'center',
                    text: [
                    { text: page.toString(), italics: true },
                    ' of ',
                    { text: pages.toString(), italics: true }
                    ]
                    }
                    ],
                    margin: [10, 0]
                    }
                    });
                
            }
        }
        
        ]
       });