使用datatables.net时导出为pdf时设置列宽

时间:2015-12-03 09:08:32

标签: pdf datatables

嗨我有这个表我想导出但是当内容很短时,一列的宽度很长,当使用datatables.net中的代码导出到pdf im时,布局看起来不太好导出时,这里是我的代码

$(document).ready(function() {
    var currentDate = new Date()
    var day = currentDate.getDate()
    var month = currentDate.getMonth() + 1
    var year = currentDate.getFullYear()

    var d = day + "-" + month + "-" + year;
    $('#detailTable').DataTable({
        dom: 'Bfrtip',
        buttons: [{
            extend: 'excelHtml5',
            title: d + ' Purchase Orders'
        }, {
            extend: 'csvHtml5',
            title: d + ' Purchase Orders'
        }, {
            extend: 'pdfHtml5',
            title: d + ' Purchase Orders',
            orientation: 'landscape',
            pageSize: 'LEGAL',
            exportOptions: {
                columns: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
            }
        }]
    });
});

有没有办法可以设置特定列的宽度?提前谢谢

1 个答案:

答案 0 :(得分:1)

您可以使用customize回调来修改PDF格式' "样式字典"。如果您想强制#2列的宽度为100px或最大100px,请执行以下操作:

{
  extend: 'pdfHtml5',
  title: d + ' Purchase Orders',
  orientation: 'landscape',
  pageSize: 'LEGAL',
  exportOptions: {
     columns: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
  },
  //-------------------------- 
  customize : function(doc) {
     doc.styles['td:nth-child(2)'] = { 
       width: '100px',
       'max-width': '100px'
     }
  }
}

这将传递CSS样式规则

td:nth-child(2) {
  width: 100px;
  max-width: 100px;
}

到PDFmake。请参阅文档 - > http://pdfmake.org/#/gettingstarted转到"样式词典"