pdfkit-table
Generate pdf tables with javascript (PDFKIT plugin)
Helps to draw informations in simple tables using pdfkit. #server-side.
Examples
view pdf example |
full code example |
server example |
json example |
both
Install
npm install pdfkit-table
Use
const fs = require("fs");
const PDFDocument = require("pdfkit-table");
const doc = new PDFDocument({ margin: 30, size: 'A4' });
doc.pipe(fs.createWriteStream("./file-table.pdf"));
const table = {
headers: [],
datas: [],
rows: [],
}
const options = {}
doc.table( table, options );
doc.end();
Example 1 - Simple Array Table
const tableArray = {
headers: ["Country", "Conversion rate", "Trend"],
rows: [
["Switzerland", "12%", "+1.12%"],
["France", "67%", "-0.98%"],
["England", "33%", "+4.44%"],
],
};
doc.table( tableArray, { width: 300 });
Example 2 - Table
const table = {
headers: [
{ label:"Name", property: 'name', width: 60, renderer: null },
{ label:"Description", property: 'description', width: 150, renderer: null },
{ label:"Price 1", property: 'price1', width: 100, renderer: null },
{ label:"Price 2", property: 'price2', width: 100, renderer: null },
{ label:"Price 3", property: 'price3', width: 80, renderer: null },
{ label:"Price 4", property: 'price4', width: 43,
renderer: (value, indexColumn, indexRow, row) => { return `U$ ${Number(value).toFixed(2)}` }
},
],
datas: [
{
name: 'Name 1',
description: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean mattis ante in laoreet egestas. ',
price1: '$1',
price3: '$ 3',
price2: '$2',
price4: '4',
},
{
options: { fontSize: 10, separation: true},
name: 'bold:Name 2',
description: 'bold:Lorem ipsum dolor.',
price1: 'bold:$1',
price3: '$3',
price2: '$2',
price4: '4',
},
{
name: 'Name 3',
description: 'Lorem ipsum dolor.',
price1: 'bold:$1',
price4: '4',
price2: '$2',
price3: {
label: 'PRICE $3', options: { fontSize: 12 }
},
},
],
rows: [
[
"Apple",
"Nullam ut facilisis mi. Nunc dignissim ex ac vulputate facilisis.",
"$ 105,99",
"$ 105,99",
"$ 105,99",
"105.99",
],
[
"Tire",
"Donec ac tincidunt nisi, sit amet tincidunt mauris. Fusce venenatis tristique quam, nec rhoncus eros volutpat nec. Donec fringilla ut lorem vitae maximus. Morbi ex erat, luctus eu nulla sit amet, facilisis porttitor mi.",
"$ 105,99",
"$ 105,99",
"$ 105,99",
"105.99",
],
],
};
doc.table(table, {
prepareHeader: () => doc.font("Helvetica-Bold").fontSize(8),
prepareRow: (row, i) => doc.font("Helvetica").fontSize(8),
});
Example 3 - Json
const tableJson = `{
"headers": [
{ "label":"Name", "property":"name", "width":100 },
{ "label":"Age", "property":"age", "width":100 },
{ "label":"Year", "property":"year", "width":100 }
],
"datas": [
{ "name":"bold:Name 1", "age":"Age 1", "year":"Year 1" },
{ "name":"Name 2", "age":"Age 2", "year":"Year 2" },
{ "name":"Name 3", "age":"Age 3", "year":"Year 3" }
],
"rows": [
["Name 4", "Age 4", "Year 4"]
],
"options": {
"width": 300
}
}`
doc.table( tableJson );
or
const json = require('./table.json');
Array.isArray(json) ?
json.forEach( table => doc.table( table, table.options || {} ) ) :
doc.table( json, json.options || {} ) ;
Example 4 - Full Code
const fs = require("fs");
const PDFDocument = require("pdfkit-table");
const doc = new PDFDocument({ margin: 30, size: 'A4', });
doc.pipe(fs.createWriteStream("./file-table.pdf"));
doc.pipe(res);
doc.end();
Table
Array.<object>
- headers
Array.<object>
| Array.[]
- label
String
- property
String
- width
Number
- renderer
Function
function(value, indexColumn, indexRow, row) { return value }
- datas
Array.<object>
- rows
Array.[]
Example code:
const table = {
headers: ['Name', 'Age'],
rows: [
['Jack', '32'],
['Maria', '30'],
]
};
const table = {
headers: [
{ label:"Name", property: 'name', width: 100, renderer: null },
{ label:"Age", property: 'age', width: 100, renderer: (value) => `U$ ${Number(value).toFixed(1)}` },
],
datas: [
{ name: 'bold:Jack', age: 32, },
{ name: 'Maria', age: { label: 30 , options: { fontSize: 12 }}, },
],
rows: [
['Jack', '32'],
['Maria', '30'],
]
};
Options
Properties | Type | Default | Description |
---|
width | Number | undefined | width of table |
x | Number | undefined / doc.x | position x (left) |
y | Number | undefined / doc.y | position y (top) |
columnSpacing | Number | 5 | |
rowSpacing | Number | 3 | |
prepareHeader | Function | Function | |
prepareRow | Function | Function | |
Example code:
const options = {
width: 500,
x: 0,
y: 0,
columnSpacing: 5,
rowSpacing: 3,
prepareHeader: () => doc.font("Helvetica-Bold").fontSize(8),
prepareRow: (row, i) => doc.font("Helvetica").fontSize(8),
}
Options Row
- separation {Booleon}
- fontSize {Number}
- fontFamily {String}
- bold:|size{n}: - 'Jack' | 'bold:Jack' | 'size11:Jack' | 'size20:Jack'
datas: [
{ name: 'Jack', options: { fontSize: 10, fontFamily: 'Courier-Bold', separation: true } },
]
datas: [
{ name: 'bold:Jack' },
]
Options Cell
- fontSize {Number}
- fontFamily {String}
datas: [
{ name: { label: 'Jack', options: { fontSize: 10, fontFamily: 'Courier-Bold' } },
]
Fonts Family
- 'Courier'
- 'Courier-Bold'
- 'Courier-Oblique'
- 'Courier-BoldOblique'
- 'Helvetica'
- 'Helvetica-Bold'
- 'Helvetica-Oblique'
- 'Helvetica-BoldOblique'
- 'Symbol'
- 'Times-Roman'
- 'Times-Bold'
- 'Times-Italic'
- 'Times-BoldItalic'
- 'ZapfDingbats'
ToDo
- renderer function on cell. Like renderer: (value) => { return
$${value}
} - load json file - require | string
- sample with database
- setFontFamily {String}
- setBoldFontFamily {String}
- verticalLines {Boolean}
- verticalLinesWidth {Number}
- verticalLinesColor {String}
- horizontalLines {Boolean}
- horizontalLinesWidth {Number}
- horizontalLinesColor {String}
- tableLine {Boolean}
- tableLineWidth {Number}
- tableLineColor {String}
- backgroundColor {String}
- striped {Boolean} (corsimcornao)
License
The MIT License.
Author
Thank you