Multiple Header Rows - pdfMake

Multiple Header Rows - pdfMake

myersc82myersc82 Posts: 3Questions: 0Answers: 0
edited February 18 in Free community support

I've seen a few posts about not being able to export multiple header rows in pdf and not being able to use colspan, so just wanted to share my fix for being able to do both. A simple modification of the buttons.html5.js file should do the trick (though I've only tested on v1.3.3.

In the action function inside pdfHtml5 extension on line 1317 change the following lines

if ( config.header ) {
    rows.push( $.map( data.header, function ( d ) {
        return {
            text: typeof d === 'string' ? d : d+'',
            style: 'tableHeader'
        };
    } ) );
}

to

if (config.header) {
    $.map(data.headerStructure, function (hr) {             
        var a = $.map(hr, function (d) {
            var h = d !== null ? {
                text: (typeof d.title === 'string' ? d.title : d.title + ''),
                style: 'tableHeader',
                colSpan: ((d.colspan === null || d.colspan === undefined) ? 1 : d.colspan)
            } : '';                 
            return h;
        });     
        rows.push(a);
    });
}

This will allow colspans and multiple header rows.

Replies

  • allanallan Posts: 65,588Questions: 1Answers: 10,904 Site admin

    Hi,

    Buttons supports multi-row headers and footers with colspan / rowspan attributes for the cells. See this example.

    I can't recall what version that was introduced in, but you are right that it won't have been in 1.3.x. Buttons 3.2.6 is the current release and I would encourage you to update to a supported version.

    Allan

  • myersc82myersc82 Posts: 3Questions: 0Answers: 0
    edited February 19

    I have the datatables.buttons-3.2.3.js file installed. This file I'm referencing is for exporting the DataTable to a file using pdfMake. So there are 3 files involved:

    core: datatables-2.3.2.js
    buttons: datatables.buttons-3.2.3.js
    file export: buttons.html5.js (v1.3.3)
    

    The export one is the one that was modified to be able to export all of the headers to the pdf file instead of the single header row it does by default, and to also allow colspan instead of printing the header cell value in each column spanned (the default).

    Looking at the source, the internal name is FileSaver.js (1.3.3).

  • allanallan Posts: 65,588Questions: 1Answers: 10,904 Site admin

    Hi,

    Thanks for your reply and the extra information. I'm not quite understanding though - the example I linked to shows multirow headers being exported by default.

    Can you give me a link to a page show the issue so I can take more of a look into it. Certainly my intention is that it should support complex headers by default.

    Allan

  • myersc82myersc82 Posts: 3Questions: 0Answers: 0

    Looking at the code for the 3.2.6 version of the pdf export in buttons.html5.js from the builder

    <script src="https://code.jquery.com/jquery-3.7.0.js" integrity="sha384-ogycHROOTGA//2Q8YUfjz1Sr7xMOJTUmY2ucsPVuXAg4CtpgQJQzGZsX768KqetU" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.2.7/pdfmake.js" integrity="sha384-P2rohseTZr3+/y/u+6xaOAE3CIkcmmC0e7ZjhdkTilUMHfNHCerfVR9KICPeFMOP" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.2.7/vfs_fonts.js" integrity="sha384-/RlQG9uf0M2vcTw3CX7fbqgbj/h8wKxw7C3zu9/GxcBPRKOEcESxaxufwRXqzq6n" crossorigin="anonymous"></script>
    <script src="https://cdn.datatables.net/2.3.7/js/dataTables.js" integrity="sha384-2FgczAMyd99nlgHFfIt2rDxfzz+8hyAkY9ufMw3A0vLlvio4vKVimC/GMFkdkbAq" crossorigin="anonymous"></script>
    <script src="https://cdn.datatables.net/buttons/3.2.6/js/dataTables.buttons.js" integrity="sha384-3dnh4WjeAOO2NzGgB/WR1N2SaJwtGmC/pcIwyTNR+P8iNqjju6gNToClL60GClS1" crossorigin="anonymous"></script>
    <script src="https://cdn.datatables.net/buttons/3.2.6/js/buttons.html5.js" integrity="sha384-FWBlOAPJ9FRQJgtP00zQNtd1wuox/BRlVTkHuOojgy2TQl+opJzVx4GhRlbaPLhB" crossorigin="anonymous"></script>
    

    the code in the file is exactly the same as the code I changed (using the headerStructure object instead of header object). Which is interesting as the version number at the top of the file is still the same (1.3.3) though I'm certain the file has changed as the date still says 2016.

    The files I was using were part of the AdminLTE3 bundle, and I updated piece by piece based on the version number and didn't update the buttons export file since the version number at the top was still the same. So you are correct, the header export does work as expected now. Guess that's why there hasn't been any comments about it in a few years.

  • allanallan Posts: 65,588Questions: 1Answers: 10,904 Site admin

    The reference to the FileSaver v1.3.3 is just part of the buttons.html5.js file, and yes, that hadn't changed for a while (I have actually updated that aspect in a dev build recently). FileSaver is bundled into buttons.html5.js, but is not all that is in that file.

    Glad we got to the bottom of it, it is working for you now, and working as expected with the current release.

    Regards,
    Allan

Sign In or Register to comment.