Tables
Conceptually tables are similar to columns. They can however have headers, borders and cells spanning over multiple columns/rows.
The table node expects a valid table value. If the value is invalid or empty, an error is thrown. This includes the table.body property as well.
var docDefinition = {
content: [
{
layout: 'lightHorizontalLines', // optional
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' ]
]
}
}
]
};
Table-cell properties
fillColor: string
: the background color of a table cellfillOpacity: string
: the background opacity of a table cell
Table layouts
Can be used layout
property for set table layout.
Available table layouts:
noBorders
headerLineOnly
lightHorizontalLines
Own table layouts
Own table layouts must be defined before calling pdfMake.createPdf(docDefinition)
.
pdfMake.tableLayouts = {
exampleLayout: {
hLineWidth: function (i, node) {
if (i === 0 || i === node.table.body.length) {
return 0;
}
return (i === node.table.headerRows) ? 2 : 1;
},
vLineWidth: function (i) {
return 0;
},
hLineColor: function (i) {
return i === 1 ? 'black' : '#aaa';
},
paddingLeft: function (i) {
return i === 0 ? 0 : 8;
},
paddingRight: function (i, node) {
return (i === node.table.widths.length - 1) ? 0 : 8;
}
}
};
// download the PDF
pdfMake.createPdf(docDefinition).download();
Alternatively, you can pass the tableLayouts directly to createPdf
without changing the global value:
- On the client side:
pdfMake.createPdf(docDefinition, tableLayouts).download();
// The full signature of createPdf looks like this.
// tableLayouts, fonts and vfs are all optional - falsy values will cause
// pdfMake.tableLayouts, pdfMake.fonts or pdfMake.vfs to be used.
pdfMake.createPdf(docDefinition, tableLayouts, fonts, vfs)
- On the server side:
var PdfPrinter = require('pdfmake');
var printer = new PdfPrinter(fonts);
// Declaring your layout
var myTableLayouts = {
exampleLayout: {
/*
Your layout here.
*/
}
};
// Building the PDF
var pdfDoc = printer.createPdfKitDocument(docDefinition, {tableLayouts: myTableLayouts});
// Writing it to disk
pdfDoc.pipe(fs.createWriteStream('document.pdf'));
pdfDoc.end();
colSpan and rowSpan
If you use colSpan or rowSpan, don’t forget to use an empty cell for the next columns or rows. See example below.
var dd = {
content: [
{
table: {
body: [
[{text: 'Header with Colspan = 2', style: 'tableHeader', colSpan: 2, alignment: 'center'}, '', {text: 'Header 3', style: 'tableHeader', alignment: 'center'}],
[{text: 'Header 1', style: 'tableHeader', alignment: 'center'}, {text: 'Header 2', style: 'tableHeader', alignment: 'center'}, {text: 'Header 3', style: 'tableHeader', alignment: 'center'}],
['Sample value 1', 'Sample value 2', 'Sample value 3'],
[{rowSpan: 3, text: 'rowSpan set to 3\nLorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor'}, 'Sample value 2', 'Sample value 3'],
['', 'Sample value 2', 'Sample value 3'],
['Sample value 1', 'Sample value 2', 'Sample value 3'],
['Sample value 1', {colSpan: 2, rowSpan: 2, text: 'Both:\nrowSpan and colSpan\ncan be defined at the same time'}, ''],
['Sample value 1', '', ''],
]
}
}
]
}
All concepts related to tables are covered by TABLES example in playground.