pdfkit-table
Advanced tools
Comparing version 0.1.29 to 0.1.31
56
index.js
@@ -18,9 +18,12 @@ "use strict"; | ||
table(table, options) { | ||
typeof table === 'string' && ( table = JSON.parse(table) ); | ||
table || (table = {}) | ||
options || (options = {}) | ||
table || (table = {}); | ||
options || (options = {}); | ||
table.headers || (table.headers = []); | ||
table.datas || (table.datas = []) | ||
table.rows || (table.rows = []) | ||
table.datas || (table.datas = []); | ||
table.rows || (table.rows = []); | ||
table.options && (options = table.options); | ||
@@ -44,3 +47,4 @@ const columnCount = table.headers.length; | ||
let rowBottomY = 0; | ||
let tableWidth = 0; | ||
this.on("pageAdded", () => { | ||
@@ -51,2 +55,6 @@ startY = this.page.margins.top; | ||
const fEval = (str) => { | ||
let f = null; eval('f = ' + str); return f; | ||
} | ||
const prepareRowOptions = (row) => { | ||
@@ -127,8 +135,17 @@ if( typeof row !== 'object' || !row.hasOwnProperty('options') ) return; | ||
let lastPosition = startX; | ||
table.headers.forEach(({label,width}, i) => { | ||
table.headers.forEach(({label, width, renderer}, i) => { | ||
// renderer && (table.headers[i].renderer = fEval(renderer)); | ||
if(renderer && typeof renderer === 'string') { | ||
table.headers[i].renderer = fEval(renderer); | ||
// console.log('A',renderer); | ||
// console.log('B',table.headers[i].renderer); | ||
} | ||
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') | ||
@@ -147,3 +164,3 @@ .fillOpacity(.1) | ||
// write | ||
this.text(label, lastPosition + 2, startY, { | ||
this.text(label, lastPosition + 0, startY, { | ||
width: width, | ||
@@ -167,6 +184,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) | ||
@@ -208,3 +227,3 @@ .stroke(); | ||
let text = row[property]; | ||
let origText = row[property]; | ||
// let origText = row[property]; | ||
@@ -214,8 +233,8 @@ // 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 | ||
} | ||
// bold | ||
if( text.indexOf('bold:') === 0 ){ | ||
if( String(text).indexOf('bold:') === 0 ){ | ||
this.font('Helvetica-Bold'); | ||
@@ -226,3 +245,3 @@ text = text.replace('bold:',''); | ||
// size | ||
if( text.indexOf('size') === 0 ){ | ||
if( String(text).indexOf('size') === 0 ){ | ||
let size = String(text).substr(4,2).replace(':','').replace('+','') >> 0; | ||
@@ -234,3 +253,3 @@ this.fontSize( size < 7 ? 7 : size ); | ||
// renderer column | ||
renderer && (text = renderer(text, index, i, row)) // text-cell, index-column, index-line, row | ||
renderer && (text = renderer(text, index, i, row)) // value, index-column, index-row, row | ||
@@ -256,3 +275,5 @@ this.text(text, posX, startY, { | ||
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) | ||
@@ -300,3 +321,4 @@ .opacity(.5) | ||
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) | ||
@@ -303,0 +325,0 @@ .opacity(.5) |
{ | ||
"name": "pdfkit-table", | ||
"version": "0.1.29", | ||
"version": "0.1.31", | ||
"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", |
@@ -12,2 +12,3 @@ # pdfkit-table | ||
[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/) | ||
@@ -33,3 +34,3 @@ | ||
// table | ||
// table | ||
const table = { | ||
@@ -50,6 +51,6 @@ headers: [], | ||
### Example 1 - Simple Array Table | ||
### Example 1 - Simple Array | ||
```js | ||
// requires | ||
const tableArray = { | ||
const table = { | ||
headers: ["Country", "Conversion rate", "Trend"], | ||
@@ -62,3 +63,3 @@ rows: [ | ||
}; | ||
doc.table( tableArray, { width: 300 }); // A4 595.28 x 841.89 (portrait) (about width sizes) | ||
doc.table( table, { width: 300 }); // A4 595.28 x 841.89 (portrait) (about width sizes) | ||
// end code | ||
@@ -138,5 +139,43 @@ ``` | ||
### Example 3 - Json | ||
### Example 3 - Full Code | ||
```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", | ||
"renderer": "function(value, i, irow){ return value + `(${(1+irow)})`; }" | ||
} | ||
], | ||
"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 | ||
@@ -162,3 +201,3 @@ const fs = require("fs"); | ||
- <code>Array.<object></code> | ||
- <code>Array.<object></code> | <code>JSON</code> | ||
- headers <code>Array.<object></code> | <code>Array.[]</code> | ||
@@ -210,4 +249,4 @@ - label <code>String</code> | ||
| **width** | <code>Number</code> | undefined | width of table | | ||
| **x** | <code>Number</code> | undefined | dox.x | position x (left) | | ||
| **y** | <code>Number</code> | undefined | dox.y | position y (top) | | ||
| **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 | | | ||
@@ -269,16 +308,16 @@ | **rowSpacing** | <code>Number</code> | 3 | | | ||
- 'Courier' | ||
- 'Courier-Bold' | ||
- 'Courier-Oblique' | ||
- 'Courier-BoldOblique' | ||
- 'Helvetica' | ||
- 'Helvetica-Bold' | ||
- 'Helvetica-Oblique' | ||
- 'Helvetica-BoldOblique' | ||
- 'Symbol' | ||
- 'Times-Roman' | ||
- 'Times-Bold' | ||
- 'Times-Italic' | ||
- 'Times-BoldItalic' | ||
- 'ZapfDingbats' | ||
- Courier | ||
- Courier-Bold | ||
- Courier-Oblique | ||
- Courier-BoldOblique | ||
- Helvetica | ||
- Helvetica-Bold | ||
- Helvetica-Oblique | ||
- Helvetica-BoldOblique | ||
- Symbol | ||
- Times-Roman | ||
- Times-Bold | ||
- Times-Italic | ||
- Times-BoldItalic | ||
- ZapfDingbats | ||
@@ -304,2 +343,10 @@ ## ToDo | ||
## Changelogs | ||
### 0.1.31 | ||
+ renderer function on json file. { "renderer": "function(value, icol, irow, row){ return (value+1) + `(${(irow+2)})`; }" } | ||
+ fix width table and separation lines size | ||
+ **Thank you, contributors!** | ||
## License | ||
@@ -306,0 +353,0 @@ |
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
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
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
21978
260
368
1