Excel and PDF export with nodejs

Excel and PDF export with nodejs

SlindSlind Posts: 9Questions: 3Answers: 0

Hi there,
I'm using the nodejs packages and are having difficulties getting the excel and pdf export to work. What I'm doing ist the following:

import $ from 'jquery';
const DataTable = require( 'datatables.net-bs' )( window, $ );
require( 'datatables.net-buttons-bs' )( window, $ );
require( 'datatables.net-buttons/js/buttons.colVis.js' )( window, $ ); // Column visibility
require( 'datatables.net-buttons/js/buttons.html5.js' )( window, $ );  // HTML 5 file export
require( 'datatables.net-buttons/js/buttons.print.js' )( window, $ );  // Print view button

$(this.refs.dataTable).DataTable({
    pageLength: 10,
    responsive: true,
    dom: '<"html5buttons"B>lTfgitp',
    buttons: [
        {extend: 'colvis'},
        {extend: 'copy'},
        {extend: 'csv'},
        {extend: 'excel'},
        {extend: 'pdf'},

        {extend: 'print',
            customize: function (win){
                $(win.document.body).addClass('white-bg');
                $(win.document.body).css('font-size', '10px');

                $(win.document.body).find('table')
                    .addClass('compact')
                    .css('font-size', 'inherit');
            }
        }
    ]

});

For some reason the excel and pdf export do not show up. (copy, csv and print work fine)

Any help would be much appreciated.

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 20,276Questions: 26Answers: 4,765

    Looks like you are missing the JSZip and PDFMake include files needed for Excel and PDF export buttons. You can use one of the download options listed here to get the additional required files:
    https://datatables.net/extensions/buttons/#Download

    Or look at the basic HTM5 example:
    https://datatables.net/extensions/buttons/examples/html5/simple.html

    Kevin

  • SlindSlind Posts: 9Questions: 3Answers: 0

    When I add these as npm dependencies it doesn't work. That is the issue at hand ;)

  • allanallan Posts: 61,667Questions: 1Answers: 10,096 Site admin
    Answer ✓

    Have you tried this:

    let jszip = require( 'jszip' );
    let pdfmake = require( 'pdfmake' );
    require( 'datatables.net-buttons/js/buttons.html5.js' )( window, $, jszip, pdfmake );
    

    That will explicitly pass the parameters through for JSZip and pdfmake.

    If that doesn't work can you link to a page showing the issue so I can take a look please?

    Allan

  • SlindSlind Posts: 9Questions: 3Answers: 0

    I haven't tried that syntax, thanks.

This discussion has been closed.