pdfkit-table
Advanced tools
Comparing version 0.1.2 to 0.1.3
89
index.js
@@ -19,21 +19,37 @@ "use strict"; | ||
let startX = this.page.margins.left, | ||
startY = this.y; | ||
typeof table === 'string' && ( table = JSON.parse(table) ); | ||
if( !options || typeof options !== 'object' ) options = {}; | ||
table || (table = {}); | ||
options || (options = {}); | ||
options.hasOwnProperty('x') && (startX = options.x); | ||
options.hasOwnProperty('y') && (startY = options.y); | ||
table.headers || (table.headers = []); | ||
table.datas || (table.datas = []); | ||
table.rows || (table.rows = []); | ||
table.options && (options = table.options); | ||
const columnCount = table.headers.length; | ||
const columnSpacing = options.columnSpacing || 5; // 15 | ||
const columnSizes = options.columnSizes || []; | ||
const columnPositions = []; // 0,10,20,30,100 | | | | | | ||
const columnPositions = []; // 0, 10, 20, 30, 100 | ||
const rowSpacing = options.rowSpacing || 3; // 5 | ||
const usableWidth = options.width || this.page.width - this.page.margins.left - this.page.margins.right; | ||
const prepareHeader = options.prepareHeader || (() => this.font("Helvetica-Bold").fontSize(8)); | ||
const prepareHeader = options.prepareHeader || (() => this.font("Helvetica-Bold").fontSize(8) ); | ||
const prepareRow = options.prepareRow || (() => this.font("Helvetica").fontSize(8) ); | ||
const prepareRowOptions = ( row ) => { | ||
const columnContainerWidth = usableWidth / columnCount; | ||
const columnWidth = columnContainerWidth - columnSpacing; | ||
const maxY = this.page.height - this.page.margins.bottom; | ||
const startX = options.x || this.page.margins.left; | ||
let startY = options.y || this.y; | ||
let rowBottomY = 0; | ||
let tableWidth = 0; | ||
this.on("pageAdded", () => { | ||
startY = this.page.margins.top; | ||
rowBottomY = 0; | ||
}); | ||
const prepareRowOptions = (row) => { | ||
if( typeof row !== 'object' || !row.hasOwnProperty('options') ) return; | ||
@@ -49,3 +65,3 @@ row.options.hasOwnProperty('fontFamily') && this.font(row.options.fontFamily); | ||
// reconhece se é uma linha object, content with property | ||
// if row is object, content with property and options | ||
if( !Array.isArray(row) && typeof row === 'object' && !row.hasOwnProperty('property') ){ | ||
@@ -87,13 +103,2 @@ const cells = []; | ||
const columnContainerWidth = usableWidth / columnCount; | ||
const columnWidth = columnContainerWidth - columnSpacing; | ||
const maxY = this.page.height - this.page.margins.bottom; | ||
let rowBottomY = 0; | ||
this.on("pageAdded", () => { | ||
startY = this.page.margins.top; | ||
rowBottomY = 0; | ||
}); | ||
// Allow the user to override style for headers | ||
@@ -128,6 +133,7 @@ prepareHeader(); | ||
width = width >> 0; // number | ||
//this.fillColor('red').strokeColor('#777777'); | ||
// background | ||
this.rect(lastPosition, startY - 5, width - 1, rowHeight + 3) | ||
this.rect(lastPosition, startY - 5, width - 0, rowHeight + 3) | ||
.fillColor('grey') | ||
@@ -146,3 +152,3 @@ .fillOpacity(.1) | ||
// write | ||
this.text(label, lastPosition + 2, startY, { | ||
this.text(label, lastPosition + 0, startY, { | ||
width: width, | ||
@@ -166,6 +172,8 @@ align: "left", | ||
rowBottomY = Math.max(startY + computeRowHeight(table.headers), rowBottomY); | ||
tableWidth = columnPositions[columnPositions.length-1] + columnSizes[columnSizes.length-1]; | ||
// Separation line between headers and rows | ||
this.moveTo(startX, rowBottomY - rowSpacing * 0.5) | ||
.lineTo(startX + usableWidth, rowBottomY - rowSpacing * 0.5) | ||
//.lineTo(startX + usableWidth, rowBottomY - rowSpacing * 0.5) | ||
.lineTo(tableWidth, rowBottomY - rowSpacing * 0.5) | ||
.lineWidth(1) | ||
@@ -179,3 +187,2 @@ .stroke(); | ||
// ------------------------------------------------------------------------------ | ||
table.datas || (table.datas = []) | ||
table.datas.forEach((row, i) => { | ||
@@ -206,6 +213,6 @@ const rowHeight = computeRowHeight(row); | ||
// Print all cells of the current row | ||
table.headers.forEach(({property,width}) => { | ||
table.headers.forEach(({property,width,renderer}, index) => { | ||
let text = row[property]; | ||
let origText = row[property]; | ||
// let origText = row[property]; | ||
@@ -215,3 +222,3 @@ // cell object | ||
text = String(text.label); // get label | ||
origText = String(text.label); // get label | ||
// origText = String(text.label); // get label | ||
row[property].hasOwnProperty('options') && prepareRowOptions(row[property]); // set style | ||
@@ -225,2 +232,3 @@ } | ||
} | ||
// size | ||
@@ -233,2 +241,5 @@ if( text.indexOf('size') === 0 ){ | ||
// renderer column | ||
renderer && (text = renderer(text, index, i, row)) // text-cell, index-column, index-line, row | ||
this.text(text, posX, startY, { | ||
@@ -253,3 +264,5 @@ width: width, | ||
this.moveTo(startX, rowBottomY - rowSpacing * 0.5) | ||
.lineTo(startX + usableWidth, rowBottomY - rowSpacing * 0.5) | ||
//.lineTo(startX + usableWidth, rowBottomY - rowSpacing * 0.5) | ||
//.lineTo(posX, rowBottomY - rowSpacing * 0.5) | ||
.lineTo(tableWidth, rowBottomY - rowSpacing * 0.5) | ||
.lineWidth(.5) | ||
@@ -269,3 +282,2 @@ .opacity(.5) | ||
// ------------------------------------------------------------------------------ | ||
table.rows || (table.rows = []) | ||
table.rows.forEach((row, i) => { | ||
@@ -282,7 +294,10 @@ const rowHeight = computeRowHeight(row); | ||
// Print all cells of the current row | ||
row.forEach((cell, i) => { | ||
row.forEach((cell, index) => { | ||
// renderer column | ||
if( typeof table.headers[index] === 'object' ){ | ||
table.headers[index].renderer && (cell = table.headers[index].renderer(cell, index, i, row)) // text-cell, index-column, index-line, row | ||
} | ||
// const posX = startX + i * columnContainerWidth; | ||
this.text(cell, columnPositions[i], startY, { | ||
width: columnSizes[i], // columnWidth | ||
this.text(cell, columnPositions[index], startY, { | ||
width: columnSizes[index], // columnWidth | ||
align: "left", | ||
@@ -297,3 +312,4 @@ }); | ||
this.moveTo(startX, rowBottomY - rowSpacing * 0.5) | ||
.lineTo(startX + usableWidth, rowBottomY - rowSpacing * 0.5) | ||
//.lineTo(startX + usableWidth, rowBottomY - rowSpacing * 0.5) | ||
.lineTo(tableWidth, rowBottomY - rowSpacing * 0.5) | ||
.lineWidth(.5) | ||
@@ -304,2 +320,5 @@ .opacity(.5) | ||
}); | ||
// ------------------------------------------------------------------------------ | ||
// rows ------------------------------------------------------------------------- | ||
// ------------------------------------------------------------------------------ | ||
@@ -306,0 +325,0 @@ this.x = startX; |
{ | ||
"name": "pdfkit-table", | ||
"version": "0.1.2", | ||
"description": "Helps to draw informations in simple tables using pdfkit. #server-side. Generate pdf tables with javascript (PDFKIT plugin) ", | ||
"version": "0.1.3", | ||
"description": "PdfKit Table. Helps to draw informations in simple tables using pdfkit. #server-side. Generate pdf tables with javascript (PDFKIT plugin) ", | ||
"main": "index.js", | ||
@@ -9,8 +9,12 @@ "scripts": { | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/natancabral/pdfkit-table.git" | ||
}, | ||
"repository": "git+https://github.com/natancabral/pdfkit-table.git", | ||
"author": "natancabral", | ||
"keywords": ["javascript","pdf","pdfkit","table"], | ||
"keywords": [ | ||
"javascript", | ||
"pdf", | ||
"pdfkit", | ||
"table", | ||
"pdfkit table", | ||
"pdfkit-table" | ||
], | ||
"license": "MIT", | ||
@@ -17,0 +21,0 @@ "bugs": { |
120
README.md
@@ -10,3 +10,5 @@ # pdfkit-table | ||
[view pdf example](https://github.com/natancabral/pdfkit-table/raw/main/example/document.pdf) | | ||
[full code example](https://github.com/natancabral/pdfkit-table/blob/main/example/index-server-example.js) | | ||
[full code example](https://github.com/natancabral/pdfkit-table/blob/main/example/index-example.js) | | ||
[server example](https://github.com/natancabral/pdfkit-table/blob/main/example/index-server-example.js) | | ||
[json example](https://github.com/natancabral/pdfkit-table/blob/main/example/index-json-example.js) | | ||
[both](https://github.com/natancabral/pdfkit-table/blob/main/example/) | ||
@@ -16,3 +18,3 @@ | ||
## Start | ||
## Install | ||
@@ -29,7 +31,20 @@ ```bash | ||
const doc = new PDFDocument({ margin: 30, size: 'A4' }); | ||
// the magic: | ||
// file name | ||
doc.pipe(fs.createWriteStream("./file-table.pdf")); | ||
// table | ||
const table = { | ||
headers: [], | ||
datas: [/* complex data */], | ||
rows: [/* or simple data */], | ||
} | ||
// options | ||
const options = {} | ||
// the magic | ||
doc.table( table, options ); | ||
//... | ||
// done! | ||
doc.end(); | ||
``` | ||
@@ -63,4 +78,6 @@ | ||
{ label:"Price 2", property: 'price2', width: 100, renderer: null }, | ||
{ label:"Price 3", property: 'price3', width: 100, renderer: null }, | ||
{ label:"Price 4", property: 'price4', width: 43, 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)}` } | ||
}, | ||
], | ||
@@ -74,6 +91,6 @@ datas: [ | ||
price2: '$2', | ||
price4: '$4', | ||
price4: '4', | ||
}, | ||
{ | ||
options: { fontSize: 10, separation: true} }, | ||
options: { fontSize: 10, separation: true}, | ||
name: 'bold:Name 2', | ||
@@ -84,3 +101,4 @@ description: 'bold:Lorem ipsum dolor.', | ||
price2: '$2', | ||
price4: '$4', | ||
price4: '4', | ||
}, | ||
{ | ||
@@ -90,3 +108,3 @@ name: 'Name 3', | ||
price1: 'bold:$1', | ||
price4: '$4', | ||
price4: '4', | ||
price2: '$2', | ||
@@ -105,3 +123,3 @@ price3: { | ||
"$ 105,99", | ||
"$ 105,99", | ||
"105.99", | ||
], | ||
@@ -114,3 +132,3 @@ [ | ||
"$ 105,99", | ||
"$ 105,99", | ||
"105.99", | ||
], | ||
@@ -126,5 +144,40 @@ ], | ||
### Example 3 - Json | ||
### Example 3 - Full Code | ||
```js | ||
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 | ||
```js | ||
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 || {} ) ; | ||
``` | ||
### Example 4 - Full Code | ||
```js | ||
// require | ||
@@ -134,3 +187,3 @@ const fs = require("fs"); | ||
const doc = new PDFDocument({ margin: 30, size: 'A4', }); | ||
// file name | ||
doc.pipe(fs.createWriteStream("./file-table.pdf")); | ||
@@ -150,3 +203,12 @@ | ||
## Table | ||
- <code>Array.<object></code> | ||
- headers <code>Array.<object></code> | <code>Array.[]</code> | ||
- label <code>String</code> | ||
- property <code>String</code> | ||
- width <code>Number</code> | ||
- renderer <code>Function</code> function(value, indexColumn, indexRow, row) { return value } | ||
- datas <code>Array.<object></code> | ||
- rows <code>Array.[]</code> | ||
Example code: | ||
@@ -168,3 +230,3 @@ ```js | ||
{ label:"Name", property: 'name', width: 100, renderer: null }, | ||
{ label:"Age", property: 'age', width: 100, renderer: null }, | ||
{ label:"Age", property: 'age', width: 100, renderer: (value) => `U$ ${Number(value).toFixed(1)}` }, | ||
], | ||
@@ -186,13 +248,13 @@ // complex content | ||
### Options Table | ||
### Options | ||
| Properties | description | | ||
-----------------------|-------------------| | ||
| **width** | width of table | | ||
| **x** | position x (left) | | ||
| **y** | position y (top) | | ||
| **columnSpacing** | 5 | | ||
| **rowSpacing** | 3 | | ||
| **prepareHeader** | Function | | ||
| **prepareRow** | Function | | ||
| Properties | Type | Default | Description | | ||
-----------------------|-----------------------|--------------------|-------------------| | ||
| **width** | <code>Number</code> | undefined | width of table | | ||
| **x** | <code>Number</code> | undefined / doc.x | position x (left) | | ||
| **y** | <code>Number</code> | undefined / doc.y | position y (top) | | ||
| **columnSpacing** | <code>Number</code> | 5 | | | ||
| **rowSpacing** | <code>Number</code> | 3 | | | ||
| **prepareHeader** | <code>Function</code> | Function | | | ||
| **prepareRow** | <code>Function</code> | Function | | | ||
@@ -267,3 +329,5 @@ | ||
- renderer function. Like renderer: (value) => { return `$${value}`} | ||
- renderer function on cell. Like renderer: (value) => { return `$${value}`} | ||
- load json file - require | string | ||
- sample with database | ||
- setFontFamily {String} | ||
@@ -270,0 +334,0 @@ - setBoldFontFamily {String} |
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
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
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
21243
251
357
1