Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adjusting Column width of DataTable pdf Export

I have already been to this thread Set Column width when exporting to pdf when using datatables net but it's almost a year old and didn't solve my problem. I want to adjust the column width of my columns when exporting the DataTable in pdf. I tried this, but nothing changed:

$('#table').dataTable({
    "dom": '<"top"f>tiB',
    paging: false,
    fixedHeader: {headerOffset: 45},
    buttons: [
    {
        extend: 'pdfHtml5',
        exportOptions: {
            columns: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
        },
        customize : function(doc) {
            doc.styles['td:nth-child(8)'] = { 
                width: '1000px',
                'max-width': '1000px'
            }
        },
        text: 'Export: PDF',
        orientation: 'landscape',
        pageSize: 'A3',
    }]
});

The pdfmake.js is included and I also tried width: 1000 or "width: 1000"and things like that.

like image 320
Manu1038 Avatar asked Nov 03 '16 10:11

Manu1038


1 Answers

I have eight columns in my table and this code works fine for me:

$.extend( true, {}, buttonCommon, {
                       extend: 'pdfHtml5',
                       orientation: 'landscape',
                       pageSize: 'A0',
                       customize: function (doc) {
                           doc.defaultStyle.fontSize = 8; //2, 3, 4,etc
                           doc.styles.tableHeader.fontSize = 10; //2, 3, 4, etc
                           doc.content[1].table.widths = [ '2%',  '14%', '14%', '14%', 
                                                           '14%', '14%', '14%', '14%'];
                       }
    });
like image 142
Prede8 Avatar answered Sep 23 '22 19:09

Prede8