Export to PDF dynamic page orientation

Export to PDF dynamic page orientation

obworleyobworley Posts: 7Questions: 2Answers: 0

I got this to work a couple of hours ago and have since broken it again. I just can't figure out what I'm doing wrong. I'm trying to dynamically change the page orientation of a pdf document based on the number of columns. I'm new to DataTables and am still trying to figure out a lot of it.

Button Print strips the br's from fields that have multiple lines and replaces with \n

var buttonPrint = { exportOptions: { format: { body: function ( data, column, row, node ) { // Strip br from rows so they will print return column ? data.replace(/<br\s*[\/]?>/gi, '\n') : data; } } } };

$.extend( true, {}, buttonPrint, { extend: 'pdfHtml5', exportOptions: {columns: ':visible', stripNewlines:false}, pageSize: 'LETTER', footer: true, customize: function(doc) { doc.styles.title.fontSize = 8; doc.defaultStyle.fontSize = 6; doc.styles.tableHeader.fontSize = 6; doc.styles.tableFooter.fontSize = 6; }, orientation: function (){ var api = this.api(); var count = api.columns().header().length; if(count> 8){ orientation = 'landscape'; }else{ orientation = 'portrait'; } } } )

Answers

  • obworleyobworley Posts: 7Questions: 2Answers: 0

    Sorry - didn't realize using the Code insert would look like that.

    var buttonPrint = {
    exportOptions: {
    format: {
    body: function ( data, column, row, node ) {
    // Strip br from rows so they will print
    return column ?
    data.replace(/<br\s*[\/]?>/gi, '\n') :
    data;
    }
    }
    }
    };

    $.extend( true, {}, buttonPrint, {
    extend: 'pdfHtml5',
    exportOptions: {columns: ':visible', stripNewlines:false},
    pageSize: 'LETTER',
    footer: true,
    customize: function(doc) {
    doc.styles.title.fontSize = 8;
    doc.defaultStyle.fontSize = 6;
    doc.styles.tableHeader.fontSize = 6;
    doc.styles.tableFooter.fontSize = 6;
    },
    orientation: function (){
    var api = this.api();
    var count = api.columns().header().length;
    if(count> 8){
    orientation = 'landscape';
    }else{
    orientation = 'portrait';
    }
    }
    } )

This discussion has been closed.