Security News
New axobject-query Maintainer Faces Backlash Over Controversial Decision to Support Legacy Node.js Versions
A JavaScript library maintainer is under fire after merging a controversial PR to support legacy versions of Node.js.
pdfkit-table
Advanced tools
Readme
Helps to draw informations in simple tables using pdfkit. #server-side.
view pdf example | color pdf | full code example | server example | json example | all
npm install pdfkit-table
const fs = require("fs");
const PDFDocument = require("pdfkit-table");
const doc = new PDFDocument({ margin: 30, size: 'A4' });
// file name
doc.pipe(fs.createWriteStream("./file-table.pdf"));
// table
const table = {
title: '',
headers: [],
datas: [/* complex data */],
rows: [/* or simple data */],
}
// options
const options = {}
// the magic
doc.table( table, options );
// done!
doc.end();
// requires
const table = {
title: "Title",
subtitle: "Subtitle",
headers: ["Country", "Conversion rate", "Trend"],
rows: [
["Switzerland", "12%", "+1.12%"],
["France", "67%", "-0.98%"],
["England", "33%", "+4.44%"],
],
};
doc.table( table, {
// A4 595.28 x 841.89 (portrait) (about width sizes)
width: 300,
//columnsSize: [ 200, 100, 100 ],
});
// end code
// require
// A4 595.28 x 841.89 (portrait) (about width sizes)
const table = {
title: "Title",
subtitle: "Subtitle",
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, indexColumn, indexRow, rectRow) => {
doc.font("Helvetica").fontSize(8);
indexColumn === 0 && doc.addBackground(rectRow, 'blue', 0.15);
},
});
// renderer function inside json file
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",
"renderer": "function(value, i, irow){ return value + `(${(1+irow)})`; }"
}
],
"rows": [
["Name 4", "Age 4", "Year 4"]
],
"options": {
"width": 300
}
}';
doc.table( tableJson );
or
const json = require('./table.json');
// if json file is array
Array.isArray(json) ?
// any tables
json.forEach( table => doc.table( table, table.options || {} ) ) :
// one table
doc.table( json, json.options || {} ) ;
// require
const fs = require("fs");
const PDFDocument = require("pdfkit-table");
const doc = new PDFDocument({ margin: 30, size: 'A4', });
// file name
doc.pipe(fs.createWriteStream("./file-table.pdf"));
// ------------------
// table code here
// ------------------
// if your run express.js server:
// HTTP response only to show pdf
doc.pipe(res);
// done
doc.end();
Array.<object>
| JSON
Array.<object>
| Array.[]
String
String
Number
String
String
String
Number
String
Object
sample: {"color": "grey", "opacity": 0.5}
String
Number
String
'green'Number
Function
function( value, indexColumn, indexRow, row, rectRow, rectCell ) { return value }Array.<object>
Array.[]
String
String
Properties | Type | Default | Description |
---|---|---|---|
label | String | undefined | description |
property | String | undefined | id |
width | Number | undefined | width of column |
align | String | left | alignment |
valign | String | undefined | vertical alignment. ex: valign: "center" |
headerColor | String | grey or #BEBEBE | color of header |
headerOpacity | Number | 0.5 | opacity of header |
headerAlign | String | left | only header |
columnColor / backgroundColor | String | undefined | color of column |
columnOpacity / backgroundOpacity | Number | undefined | opacity of column |
renderer | Function | Function | function( value, indexColumn, indexRow, row, rectRow, rectCell ) { return value } |
Example code:
const table = {
// simple headers only with ROWS (not DATAS)
headers: ['Name', 'Age'],
// simple content
rows: [
['Jack', '32'], // row 1
['Maria', '30'], // row 2
]
};
const table = {
// complex headers work with ROWS and DATAS
headers: [
{ label:"Name", property: 'name', width: 100, renderer: null },
{ label:"Age", property: 'age', width: 100, renderer: (value) => `U$ ${Number(value).toFixed(1)}` },
],
// complex content
datas: [
{ name: 'bold:Jack', age: 32, },
// age is object value with style options
{ name: 'Maria', age: { label: 30 , options: { fontSize: 12 }}, },
],
// simple content (works fine!)
rows: [
['Jack', '32'], // row 1
['Maria', '30'], // row 2
]
};
Properties | Type | Default | Description |
---|---|---|---|
title | String | undefined | title |
subtitle | String | undefined | subtitle |
width | Number | undefined | width of table |
x | Number | undefined / doc.x | position x (left) |
y | Number | undefined / doc.y | position y (top) |
columnsSize | Array | undefined | define sizes |
columnSpacing | Number | 5 | |
addPage | Boolean | false | add table on new page |
prepareHeader | Function | Function | () |
prepareRow | Function | Function | (row, indexColumn, indexRow, rectRow) => {} |
Example code:
const options = {
// properties
title: "Title",
subtitle: "Subtitle",
width: 500, // {Number} default: undefined // A4 595.28 x 841.89 (portrait) (about width sizes)
x: 0, // {Number} default: undefined | doc.x
y: 0, // {Number} default: undefined | doc.y
columnSpacing: 5, // {Number} default: 5
// functions
prepareHeader: () => doc.font("Helvetica-Bold").fontSize(8), // {Function}
prepareRow: (row, indexColumn, indexRow, rectRow) => doc.font("Helvetica").fontSize(8), // {Function}
}
{Booleon}
{Number}
{String}
datas: [
// options row
{ name: 'Jack', options: { fontSize: 10, fontFamily: 'Courier-Bold', separation: true } },
]
datas: [
// bold
{ name: 'bold:Jack' },
// size{n}
{ name: 'size20:Maria' },
{ name: 'size8:Will' },
// normal
{ name: 'San' },
]
{Number}
{String}
datas: [
// options cell | value is object | label is string
{ name: { label: 'Jack', options: { fontSize: 10, fontFamily: 'Courier-Bold' } },
]
headers: [
{label:"Name", property:"name", valign: "center", headerAlign:"right", headerColor:"#FF0000", headerOpacity:0.5 }
]
headers: [
{label:"Name", property:"name", headerColor:"#FF0000", headerOpacity:0.5 }
]
headers: [
{label:"Name", property:"name", align:"center"}
]
{Function}
- Add background peer line.
{Function}
{Function}
- Add many tables.
{Boolean}
- Add table on new page.
{String}
{String}
{Object}
{Array}
(${(irow+2)})
; }" }The MIT License.
Natan Cabral natancabral@hotmail.com https://github.com/natancabral/ |
FAQs
PdfKit Table. Helps to draw informations in simple tables using pdfkit. #server-side. Generate pdf tables with javascript (PDFKIT plugin)
The npm package pdfkit-table receives a total of 18,477 weekly downloads. As such, pdfkit-table popularity was classified as popular.
We found that pdfkit-table demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A JavaScript library maintainer is under fire after merging a controversial PR to support legacy versions of Node.js.
Security News
Results from the 2023 State of JavaScript Survey highlight key trends, including Vite's dominance, rising TypeScript adoption, and the enduring popularity of React. Discover more insights on developer preferences and technology usage.
Security News
The US Justice Department has penalized two consulting firms $11.3 million for failing to meet cybersecurity requirements on federally funded projects, emphasizing strict enforcement to protect sensitive government data.