Exporting DataTables RowGroup to PDF

Viewed 706

I understand that the feature for exporting grouped row in DataTable is still not implemented, however, I saw this code from their forums print-endrender-of-rowgroup, and I would like to ask for your help in incorporating this into the code this code, I tried running it in for loop but I'm unsuccessful in doing it.

{
                    text: 'PDF',
                    extend: 'pdfHtml5',
                    footer: true,
                    text: 'Export to PDF',
                    filename: nursingovertimedatasheet + pagename,
                    //  orientation: 'landscape', //portrait
                    //  pageSize: 'A4', //A3 , A5 , A6 , legal , letter
                    exportOptions: {
                        // columns: ':visible', //columns: ':visible:not(:last-child):not(:first-child)', colVis is now available.
                        columns: ':visible',
                        search: 'applied',
                        order: 'applied'
                    },
                    customize: function(doc) {

                        doc.content[1].table.widths =
                            Array(doc.content[1].table.body[0].length + 1).join('*').split('');

                        var tableNode;
                        for (i = 0; i < doc.content.length; ++i) {
                            if (doc.content[i].table !== undefined) {
                                tableNode = doc.content[i];
                                break;
                            }
                        }

                        var rowIndex = 0;
                        var tableColumnCount = tableNode.table.body[rowIndex].length;
                        if (tableColumnCount > 5) {
                            doc.pageOrientation = 'landscape';
                        }
                        //Remove the title created by datatTables
                        doc.content.splice(0, 1);
                        //Create a date string that we use in the footer. Format is dd-mm-yyyy
                        var now = new Date();
                        var jsDate = now.getDate() + '-' + (now.getMonth() + 1) + '-' + now.getFullYear();
                        // Logo converted to base64
                        // var logo = getBase64FromImageUrl('https://datatables.net/media/images/logo.png');
                        // The above call should work, but not when called from codepen.io
                        // So we use a online converter and paste the string in.
                        // Done on http://codebeautify.org/image-to-base64-converter
                        // It's a LONG string scroll down to see the rest of the code !!!
                        var logo = pdfhtml5;
                        var devlogo = devpdfhtml5;
                        // A documentation reference can be found at
                        // https://github.com/bpampuch/pdfmake#getting-started
                        // Set page margins [left,top,right,bottom] or [horizontal,vertical]
                        // or one number for equal spread
                        // It's important to create enough space at the top for a header !!!
                        doc.pageMargins = [20, 100, 20, 100];
                        // Set the font size fot the entire document
                        doc.defaultStyle.fontSize = 5;
                        // Set the fontsize for the table header
                        doc.styles.tableHeader.fontSize = 5;
                        // Create a header object with 3 columns
                        // Left side: Logo
                        // Middle: brandname
                        // Right side: A document title
                        doc['header'] = (function() {
                            return {
                                columns: [{
                                        image: logo,
                                        width: 52
                                    },
                                    {
                                        alignment: 'center',
                                        italics: false,
                                        bold: true,
                                        text: [{
                                            text: 'HEADER1',
                                            fontSize: 12
                                        }, {
                                            text: '\n HEADER2',
                                            fontSize: 8,
                                        }, {
                                            text: '\n\n'
                                        }, {
                                            text: nursingovertimedatasheet,
                                            fontSize: 14
                                        }, {
                                            text: ' : ',
                                            fontSize: 14
                                        }, {
                                            text: pagename,
                                            fontSize: 14
                                        }],
                                        margin: [10, 0]
                                    },
                                    {
                                        image: pdfhtml5apsysicon,
                                        width: 52
                                    }
                                ],
                                margin: 20
                            }
                        });
                        // Create a footer object with 2 columns
                        // Left side: report creation date
                        // Right side: current page and total pages
                        doc['footer'] = (function(page, pages) {
                            return {
                                columns: [{
                                        alignment: 'left',
                                        text: ['Created on: ', {
                                            text: jsDate.toString()
                                        }, {
                                            text: '\n Page '
                                        }, {
                                            text: page.toString()
                                        }, ' of ', {
                                            text: pages.toString()
                                        }],
                                        margin: [30, 0]
                                    },
                                    {
                                        alignment: 'center',
                                        stack: [{
                                            text: '***** THIS IS A COMPUTER GENERATED PAGE *****\n',
                                        }, {
                                            alignment: 'center',
                                            image: greenfooter,
                                            width: 180
                                        }],

                                    },
                                    {
                                        alignment: 'right',
                                        text: [{
                                            text: 'Prepared by: \n'
                                        }, {
                                            text: pdfhtml5preparedby
                                        }],
                                        margin: [30, 0]
                                    },
                                ],

                            }
                        });
                        // Change dataTable layout (Table styling)
                        // To use predefined layouts uncomment the line below and comment the custom lines below
                        // doc.content[0].layout = 'lightHorizontalLines'; // noBorders , headerLineOnly
                        var objLayout = {};
                        objLayout['hLineWidth'] = function(i) {
                            return .5;
                        };
                        objLayout['vLineWidth'] = function(i) {
                            return .5;
                        };
                        objLayout['hLineColor'] = function(i) {
                            return '#aaa';
                        };
                        objLayout['vLineColor'] = function(i) {
                            return '#aaa';
                        };
                        objLayout['paddingLeft'] = function(i) {
                            return 4;
                        };
                        objLayout['paddingRight'] = function(i) {
                            return 4;
                        };
                        doc.content[0].layout = objLayout;
                    }
                }

My plan is to push the newly created table into PDF by using this code from the forum print-endrender-of-rowgroup but I'm unable to do it. Is there a way to do this dynamically? The generated PDF will be overwritten by the newly checked table with grouped row?

Instead of pushing this in static, can we do it dynamically?

//Add line with data except grouped data
                if( i < doc.content[1].table.body.length-1){
                    bod.push(['','',{text:line[2].text, style:'defaultStyle'},
                                    {text:line[3].text, style:'defaultStyle'},
                                    {text:line[4].text, style:'defaultStyle'}]);
                }
                //Make last line bold, blue and a bit larger
                else{
                    bod.push(['','',{text:line[2].text, style:'lastLine'},
                                {text:line[3].text, style:'lastLine'},
                                {text:line[4].text, style:'lastLine'}]);
                }

It will instead check for the number of rows detected, and push it all.

Thank you in advance.

0 Answers
Related