pdfkit-table
Advanced tools
Comparing version 0.1.95 to 0.1.96
@@ -1,2 +0,3 @@ | ||
declare module 'pdfkit-table' { | ||
declare module 'pdfkit-table' | ||
{ | ||
import PDFDocument from 'pdfkit'; | ||
@@ -70,3 +71,6 @@ | ||
columnSpacing?: number; //default 5 | ||
padding?: number[]; | ||
addPage?: boolean; //default false | ||
hideHeader?: boolean; | ||
minRowHeight?: number; | ||
prepareHeader?: () => PDFDocumentWithTables; | ||
@@ -82,5 +86,8 @@ prepareRow?: ( | ||
export default class PDFDocumentWithTables extends PDFDocument { | ||
class PDFDocumentWithTables extends PDFDocument { | ||
public table(table: Table, options?: Options): Promise<void>; | ||
} | ||
} | ||
// export = PDFDocumentWithTables; | ||
export default PDFDocumentWithTables; | ||
} |
@@ -220,3 +220,3 @@ // jshint esversion: 6 | ||
const separationsRow = (type, x, y, width, opacity) => { | ||
const separationsRow = (type, x, y, width, opacity, color) => { | ||
@@ -235,2 +235,3 @@ type || (type = 'horizontal'); // header | horizontal | vertical | ||
width = width || options.divider[type].width || 0.5; | ||
color = color || options.divider[type].color || 'black'; | ||
@@ -242,2 +243,3 @@ // draw | ||
.lineWidth(width) | ||
.strokeColor(color) | ||
.opacity(opacity) | ||
@@ -744,3 +746,3 @@ .stroke() | ||
if(typeof renderer === 'function'){ | ||
text = renderer(text, index, i, row, rectRow, rectCell, this); // value, index-column, index-row, row, doc[this] | ||
text = renderer(text, index, i, row, rectRow, rectCell); // value, index-column, index-row, row, doc[this] | ||
} | ||
@@ -954,2 +956,3 @@ | ||
module.exports = PDFDocumentWithTables; | ||
module.exports = PDFDocumentWithTables; | ||
module.exports.default = PDFDocumentWithTables; |
{ | ||
"name": "pdfkit-table", | ||
"version": "0.1.95", | ||
"version": "0.1.96", | ||
"description": "PdfKit Table. Helps to draw informations in simple tables using pdfkit. #server-side. Generate pdf tables with javascript (PDFKIT plugin) ", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
288
README.md
@@ -43,28 +43,26 @@ <p align="center"> | ||
let doc = new PDFDocument({ margin: 30, size: 'A4' }); | ||
// save document | ||
doc.pipe(fs.createWriteStream("./document.pdf")); | ||
// table | ||
const table = { | ||
title: '', | ||
headers: [], | ||
datas: [ /* complex data */ ], | ||
rows: [ /* or simple data */ ], | ||
} | ||
;(async function(){ | ||
// table | ||
const table = { | ||
title: '', | ||
headers: [], | ||
datas: [ /* complex data */ ], | ||
rows: [ /* or simple data */ ], | ||
}; | ||
// magic | ||
doc.table(table, { /* options */ }, () => { /* callback */ } ); | ||
// or async/await | ||
// await doc.table(table); | ||
// or Promise | ||
// doc.table(table).then(() => { /* done */ }).catch((err) => { /* err */ }) | ||
// the magic (async/await) | ||
await doc.table(table, { /* options */ }, () => { /* callback */ } ); | ||
// -- or -- | ||
// doc.table(table).then(() => { doc.end() }).catch((err) => { }) | ||
// server/response | ||
// if your run express.js server | ||
// to show PDF on navigator | ||
// doc.pipe(res); | ||
// if your run express.js server | ||
// to show PDF on navigator | ||
// doc.pipe(res); | ||
// done! | ||
doc.end(); | ||
// done! | ||
doc.end(); | ||
})(); | ||
@@ -91,24 +89,26 @@ ``` | ||
```js | ||
// table | ||
const table = { | ||
title: "Title", | ||
subtitle: "Subtitle", | ||
headers: [ "Country", "Conversion rate", "Trend" ], | ||
rows: [ | ||
[ "Switzerland", "12%", "+1.12%" ], | ||
[ "France", "67%", "-0.98%" ], | ||
[ "England", "33%", "+4.44%" ], | ||
], | ||
}; | ||
// A4 595.28 x 841.89 (portrait) (about width sizes) | ||
// width | ||
doc.table(table, { | ||
width: 300, | ||
}); | ||
// or columnsSize | ||
doc.table(table, { | ||
columnsSize: [ 200, 100, 100 ], | ||
}); | ||
// done! | ||
doc.end(); | ||
;(async function(){ | ||
// table | ||
const table = { | ||
title: "Title", | ||
subtitle: "Subtitle", | ||
headers: [ "Country", "Conversion rate", "Trend" ], | ||
rows: [ | ||
[ "Switzerland", "12%", "+1.12%" ], | ||
[ "France", "67%", "-0.98%" ], | ||
[ "England", "33%", "+4.44%" ], | ||
], | ||
}; | ||
// A4 595.28 x 841.89 (portrait) (about width sizes) | ||
// width | ||
await doc.table(table, { | ||
width: 300, | ||
}); | ||
// or columnsSize | ||
await doc.table(table, { | ||
columnsSize: [ 200, 100, 100 ], | ||
}); | ||
// done! | ||
doc.end(); | ||
})(); | ||
``` | ||
@@ -119,62 +119,64 @@ | ||
```js | ||
// table | ||
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, rectRow, rectCell) => { return `U$ ${Number(value).toFixed(2)}` } | ||
;(async function(){ | ||
// table | ||
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, rectRow, rectCell) => { return `U$ ${Number(value).toFixed(2)}` } | ||
}, | ||
], | ||
// complex data | ||
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: { | ||
label: 'PRICE $3', options: { fontSize: 12 } | ||
}, | ||
price2: '$2', | ||
price4: '4', | ||
}, | ||
// {...}, | ||
], | ||
// simeple data | ||
rows: [ | ||
[ | ||
"Apple", | ||
"Nullam ut facilisis mi. Nunc dignissim ex ac vulputate facilisis.", | ||
"$ 105,99", | ||
"$ 105,99", | ||
"$ 105,99", | ||
"105.99", | ||
], | ||
// [...], | ||
], | ||
}; | ||
// the magic | ||
doc.table(table, { | ||
prepareHeader: () => doc.font("Helvetica-Bold").fontSize(8), | ||
prepareRow: (row, indexColumn, indexRow, rectRow, rectCell) => { | ||
doc.font("Helvetica").fontSize(8); | ||
indexColumn === 0 && doc.addBackground(rectRow, 'blue', 0.15); | ||
}, | ||
], | ||
// complex data | ||
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: { | ||
label: 'PRICE $3', options: { fontSize: 12 } | ||
}, | ||
price2: '$2', | ||
price4: '4', | ||
}, | ||
// {...}, | ||
], | ||
// simeple data | ||
rows: [ | ||
[ | ||
"Apple", | ||
"Nullam ut facilisis mi. Nunc dignissim ex ac vulputate facilisis.", | ||
"$ 105,99", | ||
"$ 105,99", | ||
"$ 105,99", | ||
"105.99", | ||
], | ||
// [...], | ||
], | ||
}; | ||
// the magic | ||
doc.table(table, { | ||
prepareHeader: () => doc.font("Helvetica-Bold").fontSize(8), | ||
prepareRow: (row, indexColumn, indexRow, rectRow, rectCell) => { | ||
doc.font("Helvetica").fontSize(8); | ||
indexColumn === 0 && doc.addBackground(rectRow, 'blue', 0.15); | ||
}, | ||
}); | ||
// done! | ||
doc.end(); | ||
}); | ||
// done! | ||
doc.end(); | ||
})(); | ||
@@ -186,27 +188,29 @@ ``` | ||
```js | ||
// 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", | ||
;(async function(){ | ||
// 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 | ||
} | ||
], | ||
"rows": [ | ||
[ "Name 4", "Age 4", "Year 4" ] | ||
], | ||
"options": { | ||
"width": 300 | ||
} | ||
}'; | ||
// the magic | ||
doc.table(tableJson); | ||
// done! | ||
doc.end(); | ||
}'; | ||
// the magic | ||
doc.table(tableJson); | ||
// done! | ||
doc.end(); | ||
})(); | ||
``` | ||
@@ -216,14 +220,15 @@ | ||
```js | ||
// json file | ||
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 || {}) ; | ||
// done! | ||
doc.end(); | ||
;(async function(){ | ||
// json file | ||
const json = require('./table.json'); | ||
// if json file is array | ||
Array.isArray(json) ? | ||
// any tables | ||
await doc.tables(table, table.options || {})) : | ||
// one table | ||
await doc.table(json, json.options || {}) ; | ||
// done! | ||
doc.end(); | ||
})(); | ||
``` | ||
@@ -333,4 +338,4 @@ | ||
-----------------------|-----------------------|--------------------|-------------------| | ||
| **title** | <code>String | Object</code> | undefined | title | | ||
| **subtitle** | <code>String | Object</code> | undefined | subtitle | | ||
| **title** | <code>String</code> <code>Object</code> | undefined | title | | ||
| **subtitle** | <code>String</code> <code>Object</code> | undefined | subtitle | | ||
| **width** | <code>Number</code> | undefined | width of table | | ||
@@ -342,2 +347,3 @@ | **x** | <code>Number</code> | undefined / doc.x | position x (left) | | ||
| **columnSpacing** | <code>Number</code> | 5 | | | ||
| **padding** | <code>Number</code> <code>Array</code> | 1 or [1, 5] | | | ||
| **addPage** | <code>Boolean</code> | false | add table on new page | | ||
@@ -453,10 +459,2 @@ | **hideHeader** | <code>Boolean</code> | false | hide header | | ||
``` | ||
Another alternative to alter height, inside renderer: | ||
```js | ||
// ...headers | ||
renderer: (value, indexColumn, indexRow, row, rectRow, rectCell) => { | ||
doc.positionY += 50; // rectRow.height | ||
// your code or image | ||
} | ||
``` | ||
@@ -463,0 +461,0 @@ ### 0.1.89 |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
55989
816
705