pdfkit-table
Advanced tools
Comparing version 0.1.40 to 0.1.41
304
index.js
@@ -1,2 +0,4 @@ | ||
"use strict"; | ||
// jshint esversion: 6 | ||
// "use strict"; | ||
// https://jshint.com/ | ||
@@ -18,7 +20,7 @@ const PDFDocument = require("pdfkit"); | ||
*/ | ||
addBackground = ( {x, y, width, height}, fillColor, fillOpacity, fontColor ) => { | ||
console.log('--'); | ||
addBackground = ({x, y, width, height}, fillColor, fillOpacity) => { | ||
// validate | ||
fillColor || (fillColor = 'gray'); | ||
fillOpacity || (fillOpacity = 0.1); | ||
fontColor || (fontColor = 'black'); | ||
// fontColor || (fontColor = 'black'); | ||
// set | ||
@@ -30,10 +32,10 @@ this | ||
.rect( x, y + 0.5, width, height ) | ||
.fill() | ||
//.stroke(); | ||
//.stroke() | ||
.fill(); | ||
// retore | ||
this | ||
//.restore() | ||
.fillColor('black') | ||
.fillOpacity(1) | ||
.fill() | ||
.fill(); | ||
} | ||
@@ -96,3 +98,3 @@ | ||
// get height line | ||
let cellHeight = 0; | ||
// let cellHeight = 0; | ||
// if string | ||
@@ -103,6 +105,6 @@ if(typeof data === 'string' ){ | ||
// get height line | ||
cellHeight = this.heightOfString( data, { | ||
width: usableWidth, | ||
align: "left", | ||
}); | ||
// cellHeight = this.heightOfString( data, { | ||
// width: usableWidth, | ||
// align: "left", | ||
// }); | ||
// write | ||
@@ -117,3 +119,3 @@ this.text( data, startX, startY ).opacity( 1 ); // moveDown( 0.5 ) | ||
} | ||
} | ||
}; | ||
@@ -140,46 +142,72 @@ // add a new page before crate table | ||
// warning - eval can be harmful | ||
const fEval = (str) => { | ||
let f = null; eval('f = ' + str); return f; | ||
} | ||
}; | ||
const separationsRow = (xStart, xEnd, y, strokeWidth, strokeOpacity ) => { | ||
const separationsRow = (pStart, pEnd, strokeWidth, strokeOpacity ) => { | ||
// validate | ||
strokeOpacity || (strokeOpacity = 0.5); | ||
strokeWidth || (strokeWidth = 0.5); | ||
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 ) | ||
this.moveTo(pStart.x, pStart.y - rowSpacing * 0.5) | ||
.lineTo(pEnd.x, pEnd.y - rowSpacing * 0.5) | ||
.lineWidth(strokeWidth) | ||
.opacity(strokeOpacity) | ||
.stroke() | ||
.opacity(1); // Reset opacity after drawing the line | ||
} | ||
// Reset opacity after drawing the line | ||
.opacity(1); | ||
// 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) => { | ||
// const separationsRow = (xStart, xEnd, y, strokeWidth, strokeOpacity ) => { | ||
// //validate | ||
// strokeOpacity || (strokeOpacity = 0.5); | ||
// strokeWidth || (strokeWidth = 0.5); | ||
// 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 prepareRowOptions = (row, rect) => { | ||
// 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); | ||
} | ||
}; | ||
const prepareRowBackground = (row, rect) => { | ||
// validate | ||
if( typeof row !== 'object' ) return; | ||
row.options && (row = row.options); | ||
// add backgroundColor | ||
if(row.hasOwnProperty('backgroundColor')){ | ||
const { backgroundColor, backgroundOpacity } = row; | ||
// add background | ||
this.addBackground(rect, backgroundColor, backgroundOpacity); | ||
} | ||
// add background | ||
if(row.hasOwnProperty('background')){ | ||
const { color, opacity } = row.background; | ||
// add background | ||
this.addBackground(rect, color, opacity); | ||
} | ||
}; | ||
@@ -246,3 +274,2 @@ const computeRowHeight = (row) => { | ||
columnContainerWidth = columnSizes.reduce((acc, curr, index ) => acc + curr, 0); | ||
console.log(columnContainerWidth); | ||
@@ -255,3 +282,4 @@ // background header | ||
height: rowHeight + rowSpacing, | ||
} | ||
}; | ||
// add background | ||
this.addBackground( rectRow ); | ||
@@ -278,3 +306,4 @@ | ||
height: rowHeight + rowSpacing, | ||
} | ||
}; | ||
// add background | ||
this.addBackground( rectRow ); | ||
@@ -315,3 +344,4 @@ | ||
height: rowHeight + rowSpacing, | ||
} | ||
}; | ||
// add background | ||
this.addBackground( rectRow ); | ||
@@ -342,3 +372,6 @@ | ||
// Separation line between headers and rows | ||
separationsRow( startX, tableWidth, rowBottomY, 1, 1 ); | ||
separationsRow( | ||
{x: startX, y: rowBottomY}, | ||
{x: tableWidth, y: rowBottomY}, | ||
); | ||
@@ -359,14 +392,7 @@ // data ------------------------------------------------------------------------- | ||
height: rowHeight + rowSpacing, | ||
} | ||
}; | ||
// add background | ||
//doc.addBackground(rectRow); | ||
// add background row | ||
prepareRowBackground(row, rectRow); | ||
if( row.hasOwnProperty('options') ){ | ||
if( row.options.hasOwnProperty('separation') ){ | ||
// Separation line between rows | ||
separationsRow( startX, tableWidth, rowBottomY, 1, 1); | ||
} | ||
} | ||
let posX = startX; | ||
@@ -377,2 +403,9 @@ | ||
const rectCell = { | ||
x: posX, | ||
y: startY - 5, | ||
width: width, | ||
height: rowHeight + rowSpacing, | ||
} | ||
// Allow the user to override style for rows | ||
@@ -383,9 +416,23 @@ prepareRow(row, index, i, rectRow); | ||
let text = row[property]; | ||
// let origText = row[property]; | ||
// cell object | ||
if(typeof text === 'object' ){ | ||
text = String(text.label); // get label | ||
// origText = String(text.label); // get label | ||
row[property].hasOwnProperty('options') && prepareRowOptions(row[property]); // set style | ||
// row[property].hasOwnProperty('options') && prepareRowOptions(row[property]); // set style | ||
// options if text cell is object | ||
if( row[property].hasOwnProperty('options') ){ | ||
// set font style | ||
prepareRowOptions(row[property]); | ||
prepareRowBackground(row[property], rectCell); | ||
} | ||
} else { | ||
// style column by header | ||
prepareRowBackground(table.headers[index], rectCell); | ||
} | ||
@@ -427,4 +474,21 @@ | ||
// Separation line between rows | ||
separationsRow( startX, tableWidth, rowBottomY ); | ||
// separationsRow( startX, tableWidth, rowBottomY ); | ||
separationsRow( | ||
{x: startX, y: rowBottomY}, | ||
{x: tableWidth, y: rowBottomY}, | ||
); | ||
// review this code | ||
if( row.hasOwnProperty('options') ){ | ||
if( row.options.hasOwnProperty('separation') ){ | ||
// Separation line between rows | ||
// separationsRow( startX, tableWidth, rowBottomY, 1, 1); | ||
separationsRow( | ||
{x: startX, y: rowBottomY}, | ||
{x: tableWidth, y: rowBottomY}, 1, 1 | ||
); | ||
} | ||
} | ||
}); | ||
@@ -456,6 +520,15 @@ // ------------------------------------------------------------------------------ | ||
// add background | ||
//doc.addBackground(rectRow); | ||
// doc.addBackground(rectRow); | ||
row.forEach((cell, index) => { | ||
const rectCell = { | ||
x: columnPositions[index], | ||
y: startY - 5, | ||
width: columnSizes[index], | ||
height: rowHeight + rowSpacing, | ||
} | ||
prepareRowBackground(table.headers[index], rectCell); | ||
// Allow the user to override style for rows | ||
@@ -479,3 +552,8 @@ prepareRow(row, index, i, rectRow); | ||
// Separation line between rows | ||
separationsRow( startX, tableWidth, rowBottomY ); | ||
// separationsRow( startX, tableWidth, rowBottomY ); | ||
separationsRow( | ||
{x: startX, y: rowBottomY}, | ||
{x: tableWidth, y: rowBottomY}, | ||
); | ||
@@ -512,64 +590,64 @@ }); | ||
function t2j( element ){ | ||
// function t2j( element ){ | ||
if( !element ) return; | ||
// if( !element ) return; | ||
let head = []; | ||
let data = []; | ||
// let head = []; | ||
// let data = []; | ||
const table = element; | ||
const rows = table.rows.length; | ||
let cells = 0; | ||
let text = ''; | ||
// 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); | ||
} | ||
// 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, | ||
}; | ||
// 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); | ||
// // 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 tableToJson( idElement ){ | ||
// return t2j( document.getElementById(idElement) ); | ||
// } | ||
function allTablesToJson(){ | ||
// function allTablesToJson(){ | ||
let all = []; | ||
// let all = []; | ||
const table = document.getElementsByTagName('table'); | ||
const rows = table.length; | ||
// const table = document.getElementsByTagName('table'); | ||
// const rows = table.length; | ||
for( var r = 0; r < rows; r++ ){ | ||
all.push( t2j(table[r]) ); | ||
} | ||
// for( var r = 0; r < rows; r++ ){ | ||
// all.push( t2j(table[r]) ); | ||
// } | ||
return all; | ||
// return all; | ||
} | ||
// } | ||
module.exports.tableToJson = tableToJson; | ||
module.exports.allTablesToJson = allTablesToJson; | ||
// module.exports.tableToJson = tableToJson; | ||
// module.exports.allTablesToJson = allTablesToJson; |
{ | ||
"name": "pdfkit-table", | ||
"version": "0.1.40", | ||
"version": "0.1.41", | ||
"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", |
@@ -368,6 +368,23 @@ # pdfkit-table | ||
- add line bottom or line top | ||
- backgroundColor {String} | ||
## Changelogs | ||
### 0.1.41 | ||
+ **Background** color on header to colorize ***column*** | ||
- headers: [ | ||
{ label:"Name", property: 'name', ***backgroundColor: 'red', backgroundOpacity: 0.5*** }, | ||
{ label:"Age", property: 'age', ***background: { color: 'green', opacity: 0.5 } }***, | ||
] | ||
+ **Background** color inside row options datas | ||
- datas: [ | ||
{ name:"My Name", age: 20, ***options: { backgroundColor: 'red', backgroundOpacity: 0.5 }*** }, | ||
{ name:"My Name", age: 20, ***options: { background: { color: 'green', opacity: 0.5 } }*** }, | ||
] | ||
+ **Background** color inside cell options datas | ||
- datas: [ | ||
{ name:{ label: "My Name", age: 20, ***options: { backgroundColor: 'red', backgroundOpacity: 0.5 }*** }}, | ||
{ name:{ label: "My Name", age: 20, ***options: { background: { color: 'green', opacity: 0.5 } }*** }}, | ||
] | ||
### 0.1.39 | ||
@@ -374,0 +391,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
32867
495
467