pdfkit-table
Advanced tools
Comparing version 0.1.47 to 0.1.48
184
index.js
@@ -13,2 +13,6 @@ // jshint esversion: 6 | ||
logg(...args) { | ||
// console.log(args); | ||
} | ||
/** | ||
@@ -70,5 +74,5 @@ * addBackground | ||
const columnSpacing = options.columnSpacing || 3; // 15 | ||
const columnSizes = options.columnsSize; | ||
const columnPositions = []; // 0, 10, 20, 30, 100 | ||
const usableWidth = String(options.width).replace(/[^0-9]/g,'') || this.page.width - this.page.margins.left - this.page.margins.right; | ||
let columnSizes = []; | ||
let columnPositions = []; // 0, 10, 20, 30, 100 | ||
let columnWidth = 0; | ||
@@ -80,8 +84,6 @@ const rowDistance = 0.5; | ||
let columnWidth = usableWidth / columnCount; | ||
const columnWidthFit = columnWidth - columnSpacing; | ||
const maxY = this.page.height - this.page.margins.bottom; | ||
const maxY = this.page.height - (this.page.margins.bottom + 5); | ||
let startX = options.x || this.x || this.page.margins.left; | ||
let startY = (options.y || this.y); | ||
let startY = options.y || this.y; | ||
let rowBottomY = 0; | ||
@@ -99,3 +101,3 @@ let tableWidth = 0; | ||
// Title | ||
if( !data ) return; | ||
if(!data) return; | ||
@@ -151,3 +153,3 @@ // get height line | ||
const separationsRow = (pStart, pEnd, strokeWidth, strokeOpacity) => { | ||
const separationsRow = (x, y, strokeWidth, strokeOpacity) => { | ||
@@ -158,5 +160,11 @@ // validate | ||
// distance | ||
const d = rowDistance * 1.5; | ||
// margin | ||
const m = options.x || this.page.margins.left; | ||
// draw | ||
this.moveTo(pStart.x, pStart.y - (rowDistance * 1.5)) | ||
.lineTo(pEnd.x, pEnd.y - (rowDistance * 1.5)) | ||
this | ||
.moveTo(x, y - d) | ||
.lineTo(x + tableWidth - m, y - d) | ||
.lineWidth(strokeWidth) | ||
@@ -251,2 +259,65 @@ .opacity(strokeOpacity) | ||
// Calc columns size | ||
const calcColumnsSizes = () => { | ||
// const columnPositions = []; // 0, 10, 20, 30, 100 | ||
// values | ||
// tableWidth 477 | ||
// columnPositions [ | ||
// 30, 180, 330, | ||
// 30, 180, 330, | ||
// 30, 180, 330, | ||
// ] | ||
let h = []; // header width | ||
let p = []; // position | ||
let w = 0; // table width | ||
// 1o - Max size table | ||
w = this.page.width - this.page.margins.right - ( options.x || this.page.margins.left ); | ||
// 2o - Size defined | ||
options.width && ( w = String(options.width).replace(/[^0-9]/g,'') ); | ||
// 1o | ||
table.headers.forEach( el => { | ||
el.width && h.push(el.width); // - columnSpacing | ||
}); | ||
// 2o | ||
if(h.length === 0) { | ||
h = options.columnsSize; | ||
} | ||
// 3o | ||
if(h.length === 0) { | ||
columnWidth = (w / table.headers.length); // - columnSpacing | ||
table.headers.forEach( () => h.push(columnWidth) ); | ||
} | ||
// Set columnPositions | ||
h.reduce((prev, curr, indx) => { | ||
p.push(prev >> 0); | ||
return prev + curr; | ||
},( options.x || this.page.margins.left )); | ||
// Set columnSizes | ||
h.length && (columnSizes = h); | ||
p.length && (columnPositions = p); | ||
// 3o - Sum last position + lest header width | ||
w = p[p.length-1] + h[h.length-1]; | ||
// Set tableWidth | ||
w && ( tableWidth = w ); | ||
// Ajust spacing | ||
// tableWidth = tableWidth - (h.length * columnSpacing); | ||
this.logg('columnSizes', h); | ||
this.logg('columnPositions', p); | ||
} | ||
calcColumnsSizes(); | ||
// Header | ||
@@ -270,6 +341,6 @@ | ||
if(typeof table.headers[0] === 'string' ){ | ||
if(typeof table.headers[0] === 'string') { | ||
// we have columnSizes[] complete | ||
if(columnIsDefined){ | ||
if(columnIsDefined) { | ||
@@ -288,3 +359,3 @@ // sum columns sizes | ||
// add background | ||
this.addBackground( rectRow ); | ||
this.addBackground(rectRow); | ||
@@ -301,3 +372,3 @@ lastPosition = startX; | ||
columnPositions.push(lastPosition); | ||
// columnPositions.push(lastPosition); | ||
lastPosition += columnSizes[i] >> 0; | ||
@@ -313,3 +384,3 @@ | ||
y: startY - columnSpacing - (rowDistance * 2), | ||
width: columnWidth * table.headers.length - columnSpacing, | ||
width: columnWidth * table.headers.length, | ||
height: rowHeight + columnSpacing, | ||
@@ -319,15 +390,15 @@ }; | ||
// add background | ||
this.addBackground( rectRow ); | ||
this.addBackground(rectRow); | ||
// print headers | ||
table.headers.forEach((header, i) => { | ||
table.headers.forEach( (header, i) => { | ||
lastPosition = startX + i * columnWidth; | ||
this.text(header, lastPosition, startY, { | ||
width: columnWidthFit, | ||
width: columnWidth, | ||
align: "left", | ||
}); | ||
columnSizes.push(columnWidthFit); | ||
columnPositions.push(lastPosition); | ||
// columnSizes.push(columnWidth); | ||
// columnPositions.push(lastPosition); | ||
@@ -343,4 +414,8 @@ }); | ||
// Print all headers | ||
table.headers.forEach(({label, width, renderer}, i) => { | ||
table.headers.forEach(( dataHeader, i) => { | ||
let {label, width, renderer} = dataHeader; | ||
// check defination | ||
width = width || columnWidth; | ||
if(renderer && typeof renderer === 'string') { | ||
@@ -362,3 +437,3 @@ table.headers[i].renderer = fEval(renderer); | ||
// add background | ||
this.addBackground( rectRow ); | ||
this.addBackground(rectRow); | ||
@@ -371,4 +446,4 @@ // write | ||
columnSizes.push(width); | ||
columnPositions.push(lastPosition); | ||
// columnSizes.push(width); | ||
// columnPositions.push(lastPosition); | ||
lastPosition += width; | ||
@@ -388,9 +463,9 @@ | ||
// update table width | ||
tableWidth = columnPositions[columnPositions.length-1] + columnSizes[columnSizes.length-1]; | ||
// tableWidth = columnPositions[columnPositions.length-1] + columnSizes[columnSizes.length-1]; | ||
// this.logg('tableWidth',tableWidth); | ||
// this.logg('columnPositions',columnPositions); | ||
// Separation line between headers and rows | ||
separationsRow( | ||
{x: startX, y: rowBottomY}, | ||
{x: tableWidth, y: rowBottomY}, | ||
); | ||
separationsRow(startX, rowBottomY); | ||
@@ -429,4 +504,8 @@ } | ||
// Print all cells of the current row | ||
table.headers.forEach(({property, width, renderer}, index) => { | ||
table.headers.forEach(( dataHeader, index) => { | ||
let {property, width, renderer} = dataHeader; | ||
// check defination | ||
width = width || columnWidth; | ||
const rectCell = { | ||
@@ -503,6 +582,3 @@ x: posX, | ||
// Separation line between rows | ||
separationsRow( | ||
{x: startX, y: rowBottomY}, | ||
{x: tableWidth, y: rowBottomY}, | ||
); | ||
separationsRow(startX, rowBottomY); | ||
@@ -513,6 +589,3 @@ // review this code | ||
// Separation line between rows | ||
separationsRow( | ||
{x: startX, y: rowBottomY}, | ||
{x: tableWidth, y: rowBottomY}, 1, 1 | ||
); | ||
separationsRow(startX, rowBottomY, 1, 1); | ||
} | ||
@@ -534,7 +607,8 @@ } | ||
// else this.addPage(); | ||
startY + 2 * rowHeight >= maxY && this.addPage(); | ||
if(startY + 2 * rowHeight >= maxY) this.addPage(); | ||
startY = rowBottomY + columnSpacing + rowDistance; // 0.5 is spacing rows | ||
const rectRow = { | ||
x: startX, | ||
x: columnPositions[0], | ||
// x: startX, | ||
y: startY - columnSpacing - (rowDistance * 2), | ||
@@ -547,7 +621,10 @@ width: tableWidth - startX, | ||
// doc.addBackground(rectRow); | ||
let posX = startX; | ||
row.forEach((cell, index) => { | ||
const rectCell = { | ||
x: columnPositions[index], | ||
// x: columnPositions[index], | ||
x: posX, | ||
y: startY - columnSpacing - (rowDistance * 2), | ||
@@ -564,10 +641,13 @@ width: columnSizes[index], | ||
// renderer column | ||
if( typeof table.headers[index] === 'object' ){ | ||
table.headers[index].renderer && (cell = table.headers[index].renderer(cell, index, i, row, rectRow, rectCell)) // text-cell, index-column, index-line, row | ||
if(typeof table.headers[index] === 'object') { | ||
table.headers[index].renderer && (cell = table.headers[index].renderer(cell, index, i, row, rectRow, rectCell)); // text-cell, index-column, index-line, row | ||
} | ||
// const posX = startX + i * columnWidth; | ||
this.text(cell, columnPositions[index], startY, { | ||
width: columnSizes[index], // columnWidthFit | ||
this.text(cell, posX, startY, { | ||
width: columnSizes[index], | ||
align: "left", | ||
});rectRow | ||
}); | ||
posX += columnSizes[index]; | ||
}); | ||
@@ -579,7 +659,3 @@ | ||
// Separation line between rows | ||
separationsRow( | ||
{x: startX, y: rowBottomY}, | ||
{x: tableWidth, y: rowBottomY}, | ||
); | ||
separationsRow(startX, rowBottomY); | ||
@@ -586,0 +662,0 @@ }); |
{ | ||
"name": "pdfkit-table", | ||
"version": "0.1.47", | ||
"version": "0.1.48", | ||
"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", |
@@ -371,2 +371,7 @@ # pdfkit-table | ||
### 0.1.48 | ||
+ header height size | ||
+ separate line width | ||
### 0.1.47 | ||
@@ -373,0 +378,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
33222
497
497