pdfkit-table
Advanced tools
Comparing version 0.1.37 to 0.1.39
153
index.js
@@ -12,2 +12,30 @@ "use strict"; | ||
/** | ||
* addBackground | ||
* @param {*} {object} | ||
* @param {*} fillColor | ||
* @param {*} fillOpacity | ||
* @param {*} fontColor | ||
*/ | ||
addBackground = ( {x, y, width, height}, fillColor, fillOpacity, fontColor ) => { | ||
console.log('--'); | ||
fillColor || (fillColor = 'gray'); | ||
fillOpacity || (fillOpacity = 0.1); | ||
fontColor || (fontColor = 'black'); | ||
// set | ||
this | ||
.fill(fillColor) | ||
//.stroke(fillColor) | ||
.fillOpacity(fillOpacity) | ||
.rect( x, y + 0.5, width, height ) | ||
.fill() | ||
//.stroke(); | ||
// retore | ||
this | ||
//.restore() | ||
.fillColor('black') | ||
.fillOpacity(1) | ||
.fill() | ||
} | ||
/** | ||
* table | ||
@@ -31,2 +59,3 @@ * @param {object} table | ||
options.columnsSize || (options.columnsSize = []); | ||
options.addPage || (options.addPage = false); | ||
@@ -37,3 +66,2 @@ const title = table.title ? table.title : ( options.title ? options.title : '' ) ; | ||
let columnIsDefined = options.columnsSize.length ? true : false ; | ||
const columnsCountSize = options.columnsSize.length; // pre-defined coluns size | ||
const columnCount = table.headers.length; | ||
@@ -47,3 +75,3 @@ const columnSpacing = options.columnSpacing || 5; // 15 | ||
const prepareHeader = options.prepareHeader || (() => this.font("Helvetica-Bold").fontSize(8) ); | ||
const prepareRow = options.prepareRow || (() => this.font("Helvetica").fontSize(8) ); | ||
const prepareRow = options.prepareRow || ((row, indexColumn, indexRow, rectRow) => this.font("Helvetica").fontSize(8) ); | ||
@@ -92,3 +120,5 @@ const columnContainerWidth = usableWidth / columnCount; | ||
// add a new page before crate table | ||
options.addPage && this.addPage( option ); | ||
if( options.addPage === true) { | ||
this.addPage(); // option ); | ||
} | ||
@@ -127,2 +157,23 @@ // create title and subtitle | ||
// add background | ||
// const addBackground = ( {x, y, width, height}, fillColor, fillOpacity, fontColor ) => { | ||
// fillColor || (fillColor = 'gray'); | ||
// fillOpacity || (fillOpacity = 0.1); | ||
// fontColor || (fontColor = 'black'); | ||
// // set | ||
// this | ||
// .fill(fillColor) | ||
// //.stroke(fillColor) | ||
// .fillOpacity(fillOpacity) | ||
// .rect( x, y, width, height ) | ||
// .fill() | ||
// //.stroke(); | ||
// // retore | ||
// this | ||
// //.restore() | ||
// .fillColor('black') | ||
// .fillOpacity(1) | ||
// .fill() | ||
// } | ||
const prepareRowOptions = (row) => { | ||
@@ -186,4 +237,10 @@ if( typeof row !== 'object' || !row.hasOwnProperty('options') ) return; | ||
let rowHeight = computeRowHeight(table.headers); | ||
if(typeof table.headers[0] === 'string' ){ | ||
// background header | ||
// addBackground( startX, startY - 5, 100, rowHeight + rowSpacing ); | ||
// we have columnSizes[] complete | ||
if( columnIsDefined ){ | ||
@@ -204,13 +261,14 @@ | ||
// print headers | ||
table.headers.forEach((header, i) => { | ||
// print headers | ||
table.headers.forEach((header, i) => { | ||
lastPosition = startX + i * columnContainerWidth; | ||
this.text(header, lastPosition, startY, { | ||
width: columnWidth, | ||
align: "left", | ||
lastPosition = startX + i * columnContainerWidth; | ||
this.text(header, lastPosition, startY, { | ||
width: columnWidth, | ||
align: "left", | ||
}); | ||
columnSizes.push(columnWidth); | ||
columnPositions.push(lastPosition); | ||
}); | ||
columnSizes.push(columnWidth); | ||
columnPositions.push(lastPosition); | ||
}); | ||
@@ -221,33 +279,15 @@ } | ||
let rowHeight = computeRowHeight(table.headers); | ||
// Print all headers | ||
lastPosition = startX; | ||
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 - 0, rowHeight + 3) | ||
.fillColor('grey') | ||
.fillOpacity(.1) | ||
.strokeColor('black') | ||
.strokeOpacity(1) | ||
.fill() | ||
.stroke(); | ||
// background header | ||
// addBackground( lastPosition, startY - 5, width - 0, rowHeight + 3, '#EEE', 1 ); | ||
// restore color | ||
this.fillColor('black') | ||
.fillOpacity(1) | ||
.strokeOpacity(1); | ||
// write | ||
@@ -287,6 +327,12 @@ this.text(label, lastPosition + 0, startY, { | ||
// Allow the user to override style for rows | ||
prepareRow(row, i); | ||
prepareRowOptions(row); | ||
const rectRow = { | ||
x: startX, | ||
y: startY - 5, | ||
width: tableWidth - startX, | ||
height: rowHeight + rowSpacing, | ||
} | ||
// add background | ||
//doc.addBackground(rectRow); | ||
if( row.hasOwnProperty('options') ){ | ||
@@ -300,5 +346,10 @@ if( row.options.hasOwnProperty('separation') ){ | ||
let posX = startX; | ||
// Print all cells of the current row | ||
table.headers.forEach(({property,width,renderer}, index) => { | ||
// Allow the user to override style for rows | ||
prepareRow(row, index, i, rectRow); | ||
prepareRowOptions(row); | ||
let text = row[property]; | ||
@@ -338,3 +389,3 @@ // let origText = row[property]; | ||
// if( origText.indexOf('bold:') === 0 || origText.indexOf('size') === 0 ){ | ||
prepareRow(row, i); | ||
prepareRow(row, index, i, rectRow); | ||
prepareRowOptions(row); | ||
@@ -369,6 +420,17 @@ // } | ||
// Allow the user to override style for rows | ||
prepareRow(row, i); | ||
const rectRow = { | ||
x: startX, | ||
y: startY - 5, | ||
width: tableWidth - startX, | ||
height: rowHeight + rowSpacing, | ||
} | ||
// add background | ||
//doc.addBackground(rectRow); | ||
row.forEach((cell, index) => { | ||
// Allow the user to override style for rows | ||
prepareRow(row, index, i, rectRow); | ||
// renderer column | ||
@@ -402,2 +464,17 @@ if( typeof table.headers[index] === 'object' ){ | ||
} | ||
/** | ||
* tables | ||
* @param {object} tables | ||
* @returns | ||
*/ | ||
tables(tables) { | ||
// if tables is Array | ||
Array.isArray( tables ) ? | ||
// for each on Array | ||
tables.forEach( table => this.table( table, table.options || {} ) ) : | ||
// else is tables is a unique table object | ||
( typeof tables === 'object' ? this.table( tables, tables.options || {} ) : null ) ; | ||
} | ||
} | ||
@@ -404,0 +481,0 @@ |
{ | ||
"name": "pdfkit-table", | ||
"version": "0.1.37", | ||
"version": "0.1.39", | ||
"description": "PdfKit Table. Helps to draw informations in simple tables using pdfkit. #server-side. Generate pdf tables with javascript (PDFKIT plugin) ", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"git-push": "git add . && git commit -m 'update' && git push " | ||
}, | ||
@@ -9,0 +10,0 @@ "repository": "git+https://github.com/natancabral/pdfkit-table.git", |
@@ -35,2 +35,3 @@ # pdfkit-table | ||
const table = { | ||
title: '', | ||
headers: [], | ||
@@ -77,2 +78,4 @@ datas: [/* complex data */], | ||
const table = { | ||
title: "Title", | ||
subtitle: "Subtitle", | ||
headers: [ | ||
@@ -139,3 +142,6 @@ { label:"Name", property: 'name', width: 60, renderer: null }, | ||
prepareHeader: () => doc.font("Helvetica-Bold").fontSize(8), | ||
prepareRow: (row, i) => doc.font("Helvetica").fontSize(8), | ||
prepareRow: (row, indexColumn, indexRow, rectRow) => { | ||
doc.font("Helvetica").fontSize(8); | ||
indexColumn === 0 && doc.addBackground(rectRow, 'blue', 0.15); | ||
}, | ||
}); | ||
@@ -213,2 +219,4 @@ ``` | ||
- rows <code>Array.[]</code> | ||
- title <code>String</code> | ||
- subtitle <code>String</code> | ||
@@ -261,4 +269,4 @@ Example code: | ||
| **addPage** | <code>Boolean</code> | false | add table on new page | | ||
| **prepareHeader** | <code>Function</code> | Function | | | ||
| **prepareRow** | <code>Function</code> | Function | | | ||
| **prepareHeader** | <code>Function</code> | Function | () | | ||
| **prepareRow** | <code>Function</code> | Function | (row, indexColumn, indexRow, rectRow) => {} | | ||
@@ -271,3 +279,3 @@ | ||
title: "Title", | ||
title: "Subtitle", | ||
subtitle: "Subtitle", | ||
width: 500, // {Number} default: undefined // A4 595.28 x 841.89 (portrait) (about width sizes) | ||
@@ -280,3 +288,3 @@ x: 0, // {Number} default: undefined | doc.x | ||
prepareHeader: () => doc.font("Helvetica-Bold").fontSize(8), // {Function} | ||
prepareRow: (row, i) => doc.font("Helvetica").fontSize(8), // {Function} | ||
prepareRow: (row, indexColumn, indexRow, rectRow) => doc.font("Helvetica").fontSize(8), // {Function} | ||
} | ||
@@ -349,2 +357,4 @@ ``` | ||
- [Suggestions / Issues / Fixes](https://github.com/natancabral/pdfkit-table/issues) | ||
- striped {Boolean} (corsimcornao) | ||
- colspan - the colspan attribute defines the number of columns a table cell should span. | ||
- renderer function on cell. Like renderer: (value) => { return `$${value}`} | ||
@@ -364,10 +374,23 @@ - sample with database | ||
- tableLineColor {String} | ||
- margin: marginBottom before, marginTop after | ||
- add line bottom or line top | ||
- backgroundColor {String} | ||
- striped {Boolean} (corsimcornao) | ||
## Changelogs | ||
### 0.1.39 | ||
+ **addBackground** <code>{Function}</code> - Add background peer line. | ||
- doc.addBackground( {x, y, width, height}, fillColor, opacity, fontColor ); | ||
+ **prepareRow** <code>{Function}</code> | ||
- const options = { prepareRow: (row, indexColumn, indexRow, rectRow) => { indexColumn === 0 && doc.addBackground(rectRow, 'red', 0.5) } } | ||
### 0.1.38 | ||
+ **tables** <code>{Function}</code> - Add many tables. | ||
- doc.tables([ table0, table1, table2, ... ]); | ||
### 0.1.37 | ||
+ **addPage** <code>{String}</code> - Add table on new page. | ||
+ **addPage** <code>{Boolean}</code> - Add table on new page. | ||
- const options = { addPage: true, }; | ||
@@ -436,3 +459,2 @@ | ||
- ideas - [giuseppe-santoro](https://github.com/foliojs/pdfkit/issues/29#issuecomment-56504943) | ||
- influence [voilab](https://github.com/voilab/voilab-pdf-table) | ||
- **Marcos Aurelio P. Araujo**, thanks for give me the challenge. | ||
- influence [voilab](https://github.com/voilab/voilab-pdf-table) |
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
29399
421
450