pdfkit-table
Advanced tools
Comparing version 0.0.64 to 0.0.65
74
index.js
@@ -21,5 +21,4 @@ "use strict"; | ||
startY = this.y; | ||
options || (options = {}); | ||
if( typeof options !== 'object' ) return; | ||
if( !options || typeof options !== 'object' ) options = {}; | ||
@@ -29,13 +28,11 @@ options.hasOwnProperty('x') && (startX = options.x); | ||
const columnCount = table.headers.length; | ||
const columnSpacing = options.columnSpacing || 5; // 15 | ||
const columnSizes = options.columnSizes || []; | ||
const columnCount = table.headers.length; | ||
const columnSpacing = options.columnSpacing || 5; // 15 | ||
const columnSizes = options.columnSizes || []; | ||
const columnPositions = []; // 0,10,20,30,100 | | | | | | ||
const rowSpacing = options.rowSpacing || 3; // 5 | ||
const usableWidth = | ||
options.width || | ||
this.page.width - this.page.margins.left - this.page.margins.right; | ||
const rowSpacing = options.rowSpacing || 3; // 5 | ||
const usableWidth = options.width || 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 || (() => this.font("Helvetica").fontSize(8) ); | ||
const prepareHeader = options.prepareHeader || (() => this.font("Helvetica-Bold").fontSize(8)); | ||
const prepareRow = options.prepareRow || (() => this.font("Helvetica").fontSize(8) ); | ||
@@ -95,3 +92,2 @@ const prepareRowOptions = ( row ) => { | ||
let rowBottomY = 0; | ||
// let tableMaxHeightY = 0; | ||
@@ -101,3 +97,2 @@ this.on("pageAdded", () => { | ||
rowBottomY = 0; | ||
// tableMaxHeightY = 0; | ||
}); | ||
@@ -133,4 +128,6 @@ | ||
table.headers.forEach(({label,width}, i) => { | ||
//this.fillColor('red').strokeColor('#777777'); | ||
// background | ||
this.rect(lastPosition, startY - 5, width - 1, rowHeight + 3) | ||
@@ -144,6 +141,8 @@ .fillColor('grey') | ||
this.fillColor('black') // restore color | ||
// restore color | ||
this.fillColor('black') | ||
.fillOpacity(1) | ||
.strokeOpacity(1); | ||
// write | ||
this.text(label, lastPosition + 2, startY, { | ||
@@ -168,3 +167,2 @@ width: width, | ||
rowBottomY = Math.max(startY + computeRowHeight(table.headers), rowBottomY); | ||
// console.log('rowBottomY',rowBottomY,'startY',startY,'computeRowH', computeRowHeight(table.headers)); | ||
@@ -177,2 +175,4 @@ // Separation line between headers and rows | ||
// complex data | ||
// ------------------------------------------------------------------------------ | ||
@@ -187,8 +187,4 @@ // data ------------------------------------------------------------------------- | ||
// For safety, consider 3 rows margin instead of just one | ||
if (startY + 2 * rowHeight < maxY) { | ||
startY = rowBottomY + rowSpacing; | ||
} | ||
else { | ||
this.addPage(); | ||
} | ||
if (startY + 2 * rowHeight < maxY) startY = rowBottomY + rowSpacing; | ||
else this.addPage(); | ||
@@ -213,13 +209,11 @@ // Allow the user to override style for rows | ||
table.headers.forEach(({property,width}) => { | ||
// const posX = startX + i * columnContainerWidth; | ||
// const posX = columnPositions[i]; | ||
// const cWidth = columnWidth; | ||
// const cWidth = columnSizes[i]; | ||
let text = row[property]; | ||
let origText = row[property]; | ||
// cell object | ||
if(typeof text === 'object' ){ | ||
text = String(text.label); | ||
origText = String(text.label); | ||
row[property].hasOwnProperty('options') && prepareRowOptions(row[property]); | ||
text = String(text.label); // get label | ||
origText = String(text.label); // get label | ||
row[property].hasOwnProperty('options') && prepareRowOptions(row[property]); // set style | ||
} | ||
@@ -268,2 +262,4 @@ | ||
// simple data | ||
// ------------------------------------------------------------------------------ | ||
@@ -278,8 +274,4 @@ // rows ------------------------------------------------------------------------- | ||
// For safety, consider 3 rows margin instead of just one | ||
if (startY + 2 * rowHeight < maxY) { | ||
startY = rowBottomY + rowSpacing; | ||
} | ||
else { | ||
this.addPage(); | ||
} | ||
if (startY + 2 * rowHeight < maxY) startY = rowBottomY + rowSpacing; | ||
else this.addPage(); | ||
@@ -292,12 +284,6 @@ // Allow the user to override style for rows | ||
// const posX = startX + i * columnContainerWidth; | ||
const posX = columnPositions[i]; | ||
// const cWidth = columnWidth; | ||
const cWidth = columnSizes[i]; | ||
this.text(cell, posX, startY, { | ||
width: cWidth, | ||
this.text(cell, columnPositions[i], startY, { | ||
width: columnSizes[i], // columnWidth | ||
align: "left", | ||
}); | ||
// tableMaxHeightY = this.y > tableMaxHeightY ? this.y : tableMaxHeightY; | ||
// console.log(this.y,tableMaxHeightY); | ||
}); | ||
@@ -318,4 +304,4 @@ | ||
this.x = startX; | ||
this.y = rowBottomY; //tableMaxHeightY; | ||
this.moveDown(); | ||
this.y = rowBottomY; // position y final; | ||
this.moveDown(); // break | ||
@@ -322,0 +308,0 @@ return this; |
{ | ||
"name": "pdfkit-table", | ||
"version": "0.0.64", | ||
"version": "0.0.65", | ||
"description": "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", |
123
README.md
@@ -15,3 +15,3 @@ # pdfkit-table | ||
### Start | ||
## Start | ||
@@ -22,2 +22,4 @@ ```bash | ||
## Use | ||
```js | ||
@@ -45,3 +47,3 @@ const fs = require("fs"); | ||
}; | ||
doc.moveDown().table( tableArray, { width: 300 }); // A4 595.28 x 841.89 (portrait) (about width sizes) | ||
doc.table( tableArray, { width: 300 }); // A4 595.28 x 841.89 (portrait) (about width sizes) | ||
// end code | ||
@@ -65,5 +67,28 @@ ``` | ||
datas: [ | ||
{description: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean mattis ante in laoreet egestas. ', price1: '$1', price3: '$ 3', price2: '$2', price4: '$4',name: 'Name 1', }, | ||
{name: 'bold:Name 2', description: 'bold:Lorem ipsum dolor.', price1: 'bold:$1', price3: '$3', price2: '$2', price4: '$4', options: { fontSize: 8, separation: true}}, | ||
{name: 'Name 3', description: 'Lorem ipsum dolor.', price1: 'bold:$1', price4: '$4', price2: '$2', price3: {label:'PRICE $3', options: { fontSize: 12 }}, }, | ||
{ | ||
name: 'Name 1', | ||
description: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean mattis ante in laoreet egestas. ', | ||
price1: '$1', | ||
price3: '$ 3', | ||
price2: '$2', | ||
price4: '$4', | ||
}, | ||
{ | ||
name: 'bold:Name 2', | ||
description: 'bold:Lorem ipsum dolor.', | ||
price1: 'bold:$1', | ||
price3: '$3', | ||
price2: '$2', | ||
price4: '$4', | ||
options: { fontSize: 10, separation: true} }, | ||
{ | ||
name: 'Name 3', | ||
description: 'Lorem ipsum dolor.', | ||
price1: 'bold:$1', | ||
price4: '$4', | ||
price2: '$2', | ||
price3: { | ||
label: 'PRICE $3', options: { fontSize: 12 } | ||
}, | ||
}, | ||
], | ||
@@ -102,11 +127,14 @@ rows: [ | ||
const PDFDocument = require("pdfkit-table"); | ||
const doc = new PDFDocument({ | ||
margin: 30, | ||
}); | ||
const doc = new PDFDocument({ margin: 30, size: 'A4', }); | ||
doc.pipe(fs.createWriteStream("./file-table.pdf")); | ||
// table code | ||
// ------------------ | ||
// table code here | ||
// ------------------ | ||
doc.pipe(res); // HTTP response only to show pdf | ||
// if your run express.js server: | ||
// HTTP response only to show pdf | ||
doc.pipe(res); | ||
// done | ||
doc.end(); | ||
@@ -135,7 +163,2 @@ ``` | ||
], | ||
// simple content (works fine!) | ||
rows: [ | ||
['Jack', '32'], // row 1 | ||
['Maria', '30'], // row 2 | ||
] | ||
// complex content | ||
@@ -147,2 +170,7 @@ datas: [ | ||
], | ||
// simple content (works fine!) | ||
rows: [ | ||
['Jack', '32'], // row 1 | ||
['Maria', '30'], // row 2 | ||
] | ||
}; | ||
@@ -152,3 +180,3 @@ | ||
## Options | ||
## Options TABLE | ||
@@ -171,4 +199,4 @@ | *Properties* | description | | ||
width: 500, // {Number} default: undefined // A4 595.28 x 841.89 (portrait) (about width sizes) | ||
x: 500, // {Number} default: undefined | ||
y: 500, // {Number} default: undefined | ||
x: 0, // {Number} default: undefined | doc.x | ||
y: 0, // {Number} default: undefined | doc.y | ||
columnSpacing: 5, // {Number} default: 5 | ||
@@ -182,2 +210,61 @@ rowSpacing: 3, // {Number} default: 3 | ||
#### Options Row | ||
- separation {Booleon} | ||
- fontSize {Number} | ||
- fontFamily {String} | ||
```js | ||
datas: [ | ||
// options row | ||
{ name: 'Jack', options: { fontSize: 10, fontFamily: 'Courier-Bold', separation: true } }, | ||
] | ||
``` | ||
#### Options Cell | ||
- fontSize {Number} | ||
- fontFamily {String} | ||
```js | ||
datas: [ | ||
// options cell | value is object | label is string | ||
{ name: { label: 'Jack', options: { fontSize: 10, fontFamily: 'Courier-Bold' } }, | ||
] | ||
``` | ||
#### Fonts Family | ||
- 'Courier' | ||
- 'Courier-Bold' | ||
- 'Courier-Oblique' | ||
- 'Courier-BoldOblique' | ||
- 'Helvetica' | ||
- 'Helvetica-Bold' | ||
- 'Helvetica-Oblique' | ||
- 'Helvetica-BoldOblique' | ||
- 'Symbol' | ||
- 'Times-Roman' | ||
- 'Times-Bold' | ||
- 'Times-Italic' | ||
- 'Times-BoldItalic' | ||
- 'ZapfDingbats' | ||
## ToDo | ||
- renderer function. Like renderer: (value) => { return `$${value}`} | ||
- setFontFamily {String} | ||
- setBoldFontFamily {String} | ||
- verticalLines {Boolean} | ||
- verticalLinesWidth {Number} | ||
- verticalLinesColor {String} | ||
- horizontalLines {Boolean} | ||
- horizontalLinesWidth {Number} | ||
- horizontalLinesColor {String} | ||
- tableLine {Boolean} | ||
- tableLineWidth {Number} | ||
- tableLineColor {String} | ||
- backgroundColor {String} | ||
- striped {Boolean} (corsimcornao) | ||
## License | ||
@@ -184,0 +271,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
282336
285
234