首页 > 解决方案 > 使用 DataTables 导出为 PDF 时格式化特定的列内容样式

问题描述

我在我的网站上使用Datatables,并且我试图仅在导出为 PDF 时才将特定列的内容设为粗体。我知道可以使用指定导出选项的这种方法来完成基于简单文本的格式设置,

exportOptions: {
        format: {
            body: function ( data, row, column, node ) {
                // Strip $ from salary column to make it numeric
                return column === 5 ?
                    data.replace( /[$,]/g, '' ) :
                    data;
            }
        }
    }

但我不明白如何将输出设置为粗体。谢谢你的时间!

标签: jquerycssdatatablespdf-generationpdfmake

解决方案


首先以 JSON 格式转储您的数据表内容。您可以格式化表格中任何单元格的内容。

取自 PDFMake.org 文档

var docDefinition = {
  content: [
    {
       table: {
        // headers are automatically repeated if the table spans over multiple pages
        // you can declare how many rows should be treated as headers
        headerRows: 1,
        widths: [ '*', 'auto', 100, '*' ],

        body: [
          [ 'First', 'Second', 'Third', 'The last one' ],
          [ 'Value 1', 'Value 2', 'Value 3', 'Value 4' ],
          [ { text: 'Bold value', bold: true }, 'Val 2', 'Val 3', 'Val 4' ]
        ]
      }
    }
  ]
};

pdfMake.createPdf(docDefinition).print()

推荐阅读