pdfkit-table
Advanced tools
Comparing version 0.1.42 to 0.1.43
269
index.js
@@ -15,13 +15,13 @@ // jshint esversion: 6 | ||
* addBackground | ||
* @param {*} {object} | ||
* @param {*} fillColor | ||
* @param {*} fillOpacity | ||
* @param {*} fontColor | ||
* @param {Object} rect | ||
* @param {String} fillColor | ||
* @param {Number} fillOpacity | ||
*/ | ||
addBackground = ({x, y, width, height}, fillColor, fillOpacity) => { | ||
// validate | ||
fillColor || (fillColor = 'gray'); | ||
fillOpacity || (fillOpacity = 0.1); | ||
// fontColor || (fontColor = 'black'); | ||
// set | ||
// draw bg | ||
this | ||
@@ -35,3 +35,3 @@ .fill(fillColor) | ||
// retore | ||
// restore | ||
this | ||
@@ -41,2 +41,3 @@ .fillColor('black') | ||
.fill(); | ||
} | ||
@@ -46,4 +47,4 @@ | ||
* table | ||
* @param {object} table | ||
* @param {object} options | ||
* @param {Object} table | ||
* @param {Object} options | ||
* @returns | ||
@@ -53,3 +54,3 @@ */ | ||
typeof table === 'string' && ( table = JSON.parse(table) ); | ||
typeof table === 'string' && (table = JSON.parse(table)); | ||
@@ -70,19 +71,19 @@ table || (table = {}); | ||
let columnIsDefined = options.columnsSize.length ? true : false ; | ||
const columnCount = table.headers.length; | ||
const columnIsDefined = options.columnsSize.length ? true : false ; | ||
const columnCount = table.headers.length; // TODO if not have header | ||
const columnSpacing = options.columnSpacing || 5; // 15 | ||
const columnSizes = options.columnsSize; | ||
const columnPositions = []; // 0, 10, 20, 30, 100 | ||
const rowSpacing = options.rowSpacing || 3; // 5 | ||
// const rowSpacing = options.rowSpacing || 5; // 5 | ||
const usableWidth = String(options.width).replace(/[^0-9]/g,'') || this.page.width - this.page.margins.left - this.page.margins.right; | ||
const prepareHeader = options.prepareHeader || (() => this.font("Helvetica-Bold").fontSize(8) ); | ||
const prepareRow = options.prepareRow || ((row, indexColumn, indexRow, rectRow) => this.font("Helvetica").fontSize(8) ); | ||
const prepareHeader = options.prepareHeader || (() => this.font("Helvetica-Bold").fontSize(8)); | ||
const prepareRow = options.prepareRow || ((row, indexColumn, indexRow, rectRow) => this.font("Helvetica").fontSize(8)); | ||
let columnContainerWidth = usableWidth / columnCount; | ||
const columnWidth = columnContainerWidth - columnSpacing; | ||
let columnWidth = usableWidth / columnCount; | ||
const columnWidthFit = columnWidth - columnSpacing; | ||
const maxY = this.page.height - this.page.margins.bottom; | ||
let startX = options.x || this.x || this.page.margins.left ; | ||
let startY = options.y || this.y ; | ||
let startX = options.x || this.x || this.page.margins.left; | ||
let startY = (options.y || this.y); | ||
let rowBottomY = 0; | ||
@@ -115,3 +116,3 @@ let tableWidth = 0; | ||
// startY += cellHeight; | ||
startY = this.y + 2; | ||
startY = this.y + columnSpacing + 2; | ||
// else object | ||
@@ -125,5 +126,3 @@ } else if(typeof data === 'object' ){ | ||
// add a new page before crate table | ||
if( options.addPage === true) { | ||
this.addPage(); // option ); | ||
} | ||
options.addPage === true && this.addPage(); | ||
@@ -150,3 +149,3 @@ // create title and subtitle | ||
const separationsRow = (pStart, pEnd, strokeWidth, strokeOpacity ) => { | ||
const separationsRow = (pStart, pEnd, strokeWidth, strokeOpacity) => { | ||
@@ -157,4 +156,5 @@ // validate | ||
this.moveTo(pStart.x, pStart.y - rowSpacing * 0.5) | ||
.lineTo(pEnd.x, pEnd.y - rowSpacing * 0.5) | ||
// draw | ||
this.moveTo(pStart.x, pStart.y + strokeWidth) | ||
.lineTo(pEnd.x, pEnd.y) | ||
.lineWidth(strokeWidth) | ||
@@ -168,28 +168,17 @@ .opacity(strokeOpacity) | ||
// const separationsRow = (xStart, xEnd, y, strokeWidth, strokeOpacity ) => { | ||
const prepareRowOptions = (row) => { | ||
// //validate | ||
// strokeOpacity || (strokeOpacity = 0.5); | ||
// strokeWidth || (strokeWidth = 0.5); | ||
// validate | ||
if( typeof row !== 'object' || !row.hasOwnProperty('options') ) return; | ||
// this.moveTo(xStart, y - rowSpacing * 0.5) | ||
// //.lineTo(startX + usableWidth, rowBottomY- rowSpacing * 0.5) | ||
// //.lineTo(psX, rowBottomY- rowSpacing * 0.5) | ||
// .lineTo(xEnd, y - rowSpacing * 0.5 ) | ||
// .lineWidth(strokeWidth) | ||
// .opacity(strokeOpacity) | ||
// .stroke() | ||
// .opacity(1); // Reset opacity after drawing the line | ||
// }; | ||
const {fontFamily, fontSize, color} = row.options; | ||
const prepareRowOptions = (row, rect) => { | ||
fontFamily && this.font(fontFamily); | ||
fontSize && this.fontSize(fontSize); | ||
color && this.fillColor(color); | ||
// validate | ||
if( typeof row !== 'object' || !row.hasOwnProperty('options') ) return; | ||
// row.options.hasOwnProperty('fontFamily') && this.font(row.options.fontFamily); | ||
// row.options.hasOwnProperty('fontSize') && this.fontSize(row.options.fontSize); | ||
// row.options.hasOwnProperty('color') && this.fillColor(row.options.color); | ||
row.options.hasOwnProperty('fontFamily') && this.font(row.options.fontFamily); | ||
row.options.hasOwnProperty('fontSize') && this.fontSize(row.options.fontSize); | ||
row.options.hasOwnProperty('color') && this.fillColor(row.options.color); | ||
}; | ||
@@ -202,2 +191,3 @@ | ||
// options | ||
row.options && (row = row.options); | ||
@@ -211,2 +201,3 @@ | ||
} | ||
// add background | ||
@@ -250,3 +241,2 @@ if(row.hasOwnProperty('background')){ | ||
const cellHeight = this.heightOfString(text, { | ||
// width: columnWidth, | ||
width: columnSizes[i], | ||
@@ -260,3 +250,3 @@ align: "left", | ||
return result + rowSpacing; | ||
return result + columnSpacing; | ||
}; | ||
@@ -268,3 +258,5 @@ | ||
// Check to have enough room for header and first rows. default 3 | ||
if (startY + 2 * computeRowHeight(table.headers) > maxY) this.addPage(); | ||
if (startY + 2 * computeRowHeight(table.headers) > maxY) { | ||
this.addPage(); | ||
} | ||
@@ -283,3 +275,3 @@ let lastPosition = 0; // x position head | ||
// sum columns sizes | ||
columnContainerWidth = columnSizes.reduce((acc, curr, index ) => acc + curr, 0); | ||
columnWidth = columnSizes.reduce((acc, curr, index ) => acc + curr, 0); | ||
@@ -289,6 +281,7 @@ // background header | ||
x: startX, | ||
y: startY - 5, | ||
width: columnContainerWidth, | ||
height: rowHeight + rowSpacing, | ||
y: startY - columnSpacing, | ||
width: columnWidth, | ||
height: rowHeight + columnSpacing, | ||
}; | ||
// add background | ||
@@ -298,4 +291,6 @@ this.addBackground( rectRow ); | ||
lastPosition = startX; | ||
// print headers | ||
table.headers.forEach((header, i) => { | ||
this.text(header, lastPosition, startY, { | ||
@@ -305,4 +300,6 @@ width: columnSizes[i] >> 0, | ||
}); | ||
columnPositions.push(lastPosition); | ||
lastPosition += columnSizes[i] >> 0; | ||
}); | ||
@@ -315,6 +312,7 @@ | ||
x: startX, | ||
y: startY - 5, | ||
width: columnContainerWidth * table.headers.length - 5, | ||
height: rowHeight + rowSpacing, | ||
y: startY - columnSpacing, | ||
width: columnWidth * table.headers.length - columnSpacing, | ||
height: rowHeight + columnSpacing, | ||
}; | ||
// add background | ||
@@ -326,8 +324,9 @@ this.addBackground( rectRow ); | ||
lastPosition = startX + i * columnContainerWidth; | ||
lastPosition = startX + i * columnWidth; | ||
this.text(header, lastPosition, startY, { | ||
width: columnWidth, | ||
width: columnWidthFit, | ||
align: "left", | ||
}); | ||
columnSizes.push(columnWidth); | ||
columnSizes.push(columnWidthFit); | ||
columnPositions.push(lastPosition); | ||
@@ -341,4 +340,5 @@ | ||
lastPosition = startX; | ||
// Print all headers | ||
lastPosition = startX; | ||
table.headers.forEach(({label, width, renderer}, i) => { | ||
@@ -350,3 +350,4 @@ | ||
width = width >> 0; // number | ||
// force number | ||
width = width >> 0; | ||
@@ -356,6 +357,7 @@ // background header | ||
x: lastPosition, | ||
y: startY - 5, | ||
y: startY - columnSpacing, | ||
width: width, | ||
height: rowHeight + rowSpacing, | ||
height: rowHeight + columnSpacing, | ||
}; | ||
// add background | ||
@@ -369,11 +371,11 @@ this.addBackground( rectRow ); | ||
}) | ||
columnSizes.push(width); | ||
columnPositions.push(lastPosition); | ||
lastPosition += width; | ||
}); | ||
if( table.headers.hasOwnProperty('options') ){ | ||
table.headers.options.hasOwnProperty('fontFamily') && this.font(table.headers.options.fontFamily); | ||
table.headers.options.hasOwnProperty('fontSize') && this.fontSize(table.headers.options.fontSize); | ||
} | ||
// set style | ||
prepareRowOptions(table.headers); | ||
@@ -385,2 +387,4 @@ } | ||
rowBottomY = Math.max(startY + computeRowHeight(table.headers), rowBottomY); | ||
// update table width | ||
tableWidth = columnPositions[columnPositions.length-1] + columnSizes[columnSizes.length-1]; | ||
@@ -394,3 +398,4 @@ | ||
// data ------------------------------------------------------------------------- | ||
// datas ---------------------------------------------------- | ||
table.datas.forEach((row, i) => { | ||
@@ -401,3 +406,3 @@ const rowHeight = computeRowHeight(row); | ||
// For safety, consider 3 rows margin instead of just one | ||
if (startY + 2 * rowHeight < maxY) startY = rowBottomY + rowSpacing; | ||
if (startY + 2 * rowHeight < maxY) startY = rowBottomY + columnSpacing; | ||
else this.addPage(); | ||
@@ -407,5 +412,5 @@ | ||
x: startX, | ||
y: startY - 5, | ||
y: startY - columnSpacing, | ||
width: tableWidth - startX, | ||
height: rowHeight + rowSpacing, | ||
height: rowHeight + columnSpacing, | ||
}; | ||
@@ -419,14 +424,14 @@ | ||
// Print all cells of the current row | ||
table.headers.forEach(({property,width,renderer}, index) => { | ||
table.headers.forEach(({property, width, renderer}, index) => { | ||
const rectCell = { | ||
x: posX, | ||
y: startY - 5, | ||
y: startY - columnSpacing, | ||
width: width, | ||
height: rowHeight + rowSpacing, | ||
height: rowHeight + columnSpacing, | ||
} | ||
// Allow the user to override style for rows | ||
// allow the user to override style for rows | ||
prepareRowOptions(row); | ||
prepareRow(row, index, i, rectRow); | ||
prepareRowOptions(row); | ||
@@ -479,7 +484,5 @@ let text = row[property]; | ||
// repare font family | ||
// if( origText.indexOf('bold:') === 0 || origText.indexOf('size') === 0 ){ | ||
prepareRow(row, index, i, rectRow); | ||
prepareRowOptions(row); | ||
// } | ||
// set style | ||
prepareRowOptions(row); | ||
prepareRow(row, index, i, rectRow); | ||
@@ -492,3 +495,2 @@ }); | ||
// Separation line between rows | ||
// separationsRow( startX, tableWidth, rowBottomY ); | ||
separationsRow( | ||
@@ -503,3 +505,2 @@ {x: startX, y: rowBottomY}, | ||
// Separation line between rows | ||
// separationsRow( startX, tableWidth, rowBottomY, 1, 1); | ||
separationsRow( | ||
@@ -514,11 +515,5 @@ {x: startX, y: rowBottomY}, | ||
}); | ||
// ------------------------------------------------------------------------------ | ||
// end data --------------------------------------------------------------------- | ||
// ------------------------------------------------------------------------------ | ||
// end datas | ||
// simple data | ||
// ------------------------------------------------------------------------------ | ||
// rows ------------------------------------------------------------------------- | ||
// ------------------------------------------------------------------------------ | ||
// rows ---------------------------------------------------- | ||
table.rows.forEach((row, i) => { | ||
@@ -529,3 +524,3 @@ const rowHeight = computeRowHeight(row); | ||
// For safety, consider 3 rows margin instead of just one | ||
if (startY + 2 * rowHeight < maxY) startY = rowBottomY + rowSpacing; | ||
if (startY + 2 * rowHeight < maxY) startY = rowBottomY + columnSpacing; | ||
else this.addPage(); | ||
@@ -535,5 +530,5 @@ | ||
x: startX, | ||
y: startY - 5, | ||
y: startY - columnSpacing, | ||
width: tableWidth - startX, | ||
height: rowHeight + rowSpacing, | ||
height: rowHeight + columnSpacing, | ||
} | ||
@@ -548,5 +543,5 @@ | ||
x: columnPositions[index], | ||
y: startY - 5, | ||
y: startY - columnSpacing, | ||
width: columnSizes[index], | ||
height: rowHeight + rowSpacing, | ||
height: rowHeight + columnSpacing, | ||
} | ||
@@ -563,5 +558,5 @@ | ||
} | ||
// const posX = startX + i * columnContainerWidth; | ||
// const posX = startX + i * columnWidth; | ||
this.text(cell, columnPositions[index], startY, { | ||
width: columnSizes[index], // columnWidth | ||
width: columnSizes[index], // columnWidthFit | ||
align: "left", | ||
@@ -575,3 +570,2 @@ }); | ||
// Separation line between rows | ||
// separationsRow( startX, tableWidth, rowBottomY ); | ||
separationsRow( | ||
@@ -584,6 +578,5 @@ {x: startX, y: rowBottomY}, | ||
}); | ||
// ------------------------------------------------------------------------------ | ||
// rows ------------------------------------------------------------------------- | ||
// ------------------------------------------------------------------------------ | ||
// end rows ---------------------------------------------------- | ||
// update position | ||
this.x = startX; | ||
@@ -598,3 +591,3 @@ this.y = rowBottomY; // position y final; | ||
* tables | ||
* @param {object} tables | ||
* @param {Object} tables | ||
* @returns | ||
@@ -604,3 +597,3 @@ */ | ||
// if tables is Array | ||
Array.isArray( tables ) ? | ||
Array.isArray(tables) ? | ||
// for each on Array | ||
@@ -614,66 +607,2 @@ tables.forEach( table => this.table( table, table.options || {} ) ) : | ||
module.exports = PDFDocumentWithTables; | ||
// function t2j( element ){ | ||
// if( !element ) return; | ||
// let head = []; | ||
// let data = []; | ||
// const table = element; | ||
// const rows = table.rows.length; | ||
// let cells = 0; | ||
// let text = ''; | ||
// for( var r = 0; r < rows; r++ ){ | ||
// cells || (cells = table.rows[0].cells.length); | ||
// let simpleRow = []; | ||
// for( var c = 0; c < cells; c++ ){ | ||
// text = table.rows[r].cells[c].textContent; | ||
// if( r === 0 ) { | ||
// head.push(text); | ||
// }else { | ||
// simpleRow.push(text); | ||
// } | ||
// } | ||
// simpleRow.length && data.push(simpleRow); | ||
// } | ||
// return { | ||
// headers: head, | ||
// datas: [], | ||
// rows: data, | ||
// }; | ||
// // console.log(head, data); | ||
// // console.log(table.rows); | ||
// // console.log(table.rows.length); | ||
// // console.log(table.rows[0].cells); | ||
// // console.log(table.rows[0].cells[0].textContent); | ||
// // console.log(table.rows[0].cells[0].cellIndex); | ||
// // console.log(table.rows[0].innerHTML); | ||
// } | ||
// function tableToJson( idElement ){ | ||
// return t2j( document.getElementById(idElement) ); | ||
// } | ||
// function allTablesToJson(){ | ||
// let all = []; | ||
// const table = document.getElementsByTagName('table'); | ||
// const rows = table.length; | ||
// for( var r = 0; r < rows; r++ ){ | ||
// all.push( t2j(table[r]) ); | ||
// } | ||
// return all; | ||
// } | ||
// module.exports.tableToJson = tableToJson; | ||
// module.exports.allTablesToJson = allTablesToJson; | ||
module.exports = PDFDocumentWithTables; |
{ | ||
"name": "pdfkit-table", | ||
"version": "0.1.42", | ||
"version": "0.1.43", | ||
"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", |
@@ -372,2 +372,7 @@ # pdfkit-table | ||
### 0.1.43 | ||
+ Remove **rowSpacing** | ||
+ Fix **columnSpacing** | ||
### 0.1.41 | ||
@@ -374,0 +379,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
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
473
30112
426