PDF Export a Table in a Table

PDF Export a Table in a Table

Andreas S.Andreas S. Posts: 208Questions: 74Answers: 4

I need a PDF export of an datatable that have an table in a cell. I try to do this with the following code:

dtButtons.push( {
                extend: 'pdfHtml5',
                text: '<i class="fas fa-file-pdf text-red fa-fw"></i>',
                customize: function( doc ) {
                    var name = mt_fee.column(1).data().toArray();
                    var sex = mt_fee.column(2).data().toArray();
                    var yob = mt_fee.column(3).data().toArray();
                    var events = mt_fee.column(4).data().toArray();
                    var events_lenght = events.length;
                    var rows = [];
                    var entries = [];
                    var ent = [];
                    
                    for( var i = 0; i < events.length; i++ ) {
                        var subtablerows = ( 0 == events[i].length % 2 ) ? Math.floor( events[i].length / 2 >> 0 ) : Math.floor( events[i].length / 2 >> 0 ) + 1;
                        var row_remainer = events[i].length % 2;
                        var entriesnr = events[i].length;
                        var nr0 = 0;
                        var nr1 = 1;
                        for( var j = 0; j < ( subtablerows - 1 ); j++ ) {
                            if( 'undefined' !== events[i][nr0].distance && 'undefined' !== events[i][nr1].distance ) {
                                entries[j] = [
                                    events[i][nr0].distance + 'm ' + events[i][nr0].stroke + ' (' + events[i][nr0].eventNumber + ')',
                                    events[i][nr0].entrytime,
                                    events[i][nr1].distance + 'm ' + events[i][nr1].stroke + ' (' + events[i][nr1].eventNumber + ')',
                                    events[i][nr1].entrytime
                                ];
                            } else if( 'undefined' !== events[i][nr0].distance && 'undefined' === events[i][nr1].distance ) {
                                entries[j] = [
                                    events[i][nr0].distance + 'm ' + events[i][nr0].stroke + ' (' + events[i][nr0].eventNumber + ')',
                                    events[i][nr0].entrytime,
                                    '',
                                    ''
                                ];
                            }
                            nr0++;
                            nr1++;
                        }
                        //console.log( entries );
                        ent[i] = [ {
                            table: {
                                body: entries
                            }
                        } ];
                        console.log( ent[i] );
                        rows[i] = [ name[i], ( 'FEMALE' == sex[i] ) ? 'F' : 'M', yob[i], [ ent[i] ] ];
                    }
                    console.log(rows);
                    var objMain = {
                        table: {
                            body: [
                                rows
                            ]
                        }
                    };
                    
                    doc.content[1].table.body = rows;
                }

            } );

My problem is, only one table object are include in one cell and it is the wrong one. in the oder cells i see empty tables. I have attached a result file that you can see my problem. Any Idea where my fault ist?

Andreas

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,678Questions: 1Answers: 10,497 Site admin
    Answer ✓

    To get the data in the current order you'll probably need to use:

    mt_fee.column(1, { order: 'current' }).data().toArray()
    

    since the default is to return the data in index order (while the PDF is created in display order).

    I don't really know enough about pdfmake to comment about the other aspect though I'm afraid. Can you even do that in pdfmake?

    Allan

This discussion has been closed.