如何更改数据表中的导出PDF字体大小?

时间:2015-11-30 12:03:28

标签: pdf datatables font-size

我有一个数据表,我有打印按钮和pdf按钮。 我可以在打印页面时更改字体大小,但在导出pdf文件时我无法更改字体大小。

    {
        extend: 'pdfHtml5',
        text: 'Save PDF',
        exportOptions: {
            modifier: {
                page: 'current'
            }
        },
        header: true,
        title: 'My Table Title',
        orientation: 'landscape'
    }, 
    {
        extend: 'print',
        text: 'Print',
            customize: function ( win ) {
                $(win.document.body)
                    .css( 'font-size', '15pt' )


                $(win.document.body).find( 'table' )
                    .addClass( 'compact' )
                    .css( 'font-size', '8pt' );
            },
            header: false,
            title: '',
            orientation: 'landscape'
     },
你可以帮帮我吗?

1 个答案:

答案 0 :(得分:24)

如果查看html5.js的src,它会创建一个默认对象文字doc,其中包含默认设置:

var doc = {
    pageSize: config.pageSize,
    pageOrientation: config.orientation,
    content: [
        {
            table: {
                headerRows: 1,
                body: rows
            },
            layout: 'noBorders'
        }
    ],
    styles: {
        tableHeader: {
            bold: true,
            fontSize: 11,
            color: 'white',
            fillColor: '#2d4154',
            alignment: 'center'
        },
        tableBodyEven: {},
        tableBodyOdd: {
            fillColor: '#f3f3f3'
        },
        tableFooter: {
            bold: true,
            fontSize: 11,
            color: 'white',
            fillColor: '#2d4154'
        },
        title: {
            alignment: 'center',
            fontSize: 15
        },
        message: {}
    },
    defaultStyle: {
        fontSize: 10
    }
};

您可以使用customize回调来更改这些默认设置:

{
   extend: 'pdfHtml5',
   text: 'Save PDF',
   exportOptions: {
      modifier: {
         page: 'current'
      }
   },
   header: true,
   title: 'My Table Title',
   orientation: 'landscape',
   customize: function(doc) {
      doc.defaultStyle.fontSize = 16; //<-- set fontsize to 16 instead of 10 
   }  
}, 

更改标题字体大小:

doc.styles.tableHeader.fontSize = 30    

<强> https://jsfiddle.net/2nwqa2jk/12/

更改对齐方式,将中心设置为所有标题,所有列均为所有页脚:

doc.defaultStyle.alignment = 'center'

<强> https://jsfiddle.net/2nwqa2jk/13/