Add custom columns to PDF/Export/Print?

Add custom columns to PDF/Export/Print?

resqonlineresqonline Posts: 58Questions: 14Answers: 0

Is it possible to add columns without any special value from the datatable to an export/pdf/print file?

So for example: When exporting all confirmed participants from a list, I want the columns to be:
row index number (how can I add that?), name (from table), some other column from table, additional column with custom header text, second additional column

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,121Questions: 1Answers: 10,397 Site admin
    Answer ✓

    Do you mean for a column that doesn't exist in the DataTable? If so, you would need to use the customizeData callback for each button type to modify the data retrieved for the button and add the extra column (a simple loop and an unshift to add an index should do it).

    Allan

  • resqonlineresqonline Posts: 58Questions: 14Answers: 0

    Oh dear! I might be easier off with doing an ajax call and passing it to a PDF making function I already use :D Otherwise I would spent hours learning how to customize a not so nice looking PDF with PDF make... the client wouldn't like that.

  • allanallan Posts: 63,121Questions: 1Answers: 10,397 Site admin

    If you already have a PDF generator, I'd agree - go with that.

    I've actually been strongly thinking about dropping pdfmake entirely and just asking users to do "Print to PDF" in their browsers...

    Allan

  • resqonlineresqonline Posts: 58Questions: 14Answers: 0

    I've now managed to get a custom export PDF by using an ajax call to get the data and columns I want to display and simply edit the whole PDF process on the go:

    buttons: [
        { 
            text: "Teilnehmerliste PDF",
            extend: 'pdfHtml5',
            action: function ( e, dt, node, conf ) {
                $.ajax({
                    url: datatablesajax.url,
                    type: 'POST',
                    data: {
                        action: 'getteilnehmerliste',
                        data: {
                            kursnummer: kursnummer,
                        },  
                    },
                    dataType: "json",
                }).done(function( data ){
                        var rows = [];
                        rows.push( $.map( data.header, function ( d ) {
                            return {
                                text: typeof d === 'string' ? d : d+'',
                                style: 'tableHeader'
                            };
                        } ) );
                        for ( var i=0, ien=data.body.length ; i<ien ; i++ ) {
                            rows.push( $.map( data.body[i], function ( d ) {
                                if ( d === null || d === undefined ) {
                                    d = '';
                                }
                                return {
                                    text: typeof d === 'string' ? d : d+'',
                                    style: i % 2 ? 'tableBodyEven' : 'tableBodyOdd'
                                };
                            } ) );
                        }
                    var doc = {
                        pageSize: 'A4',
                        pageOrientation: 'portrait',
                        info: {
                            title: 'Teilnehmerliste ' + kursnummer,
                        },
                        content: [
                            {
                                text: 'Teilnehmerliste ' + kursnummer,
                                style: 'title',
                            },
                            {
                                table: {
                                    headerRows: 1,
                                    widths: ['auto', 'auto', '*', 'auto'],
                                    body: rows
                                },
                                layout: 'lightHorizontalLines',
                            }
                        ],
                        styles: {
                            tableHeader: {
                                bold: true,
                                fontSize: 11,
                                alignment: 'left'
                            },
                            tableBodyEven: {},
                            tableBodyOdd: {
                                fillColor: '#f3f3f3'
                            },
                            tableFooter: {
                                bold: true,
                                fontSize: 11,
                            },
                            title: {
                                alignment: 'center',
                                fontSize: 18
                            },
                            message: {},
                            lineHeight: 1.2,
                        },
                        defaultStyle: {
                            fontSize: 10
                        },
                    };
                    var pdf = pdfMake.createPdf( doc );
                    pdf.open();
                });
            },
        },
    ],
    
  • allanallan Posts: 63,121Questions: 1Answers: 10,397 Site admin

    want to display and simply edit the whole PDF process on the go

    Sorry - not clear if that is a question or not? It looks like you've got a way to make Ajax retrieved data to work with pdfmake there.

    Allan

Sign In or Register to comment.