pdfkit-table-ts
Advanced tools
Comparing version 0.0.13 to 0.0.14
declare const PDFDocumentSource: any; | ||
import { IData, IDivider, IHeader, IOptions, IPadding, IRect, ITable, ITitle } from "./types"; | ||
interface IIFillAndOpacity { | ||
declare type IICellPaddingInput = number | number[] | IPadding; | ||
declare interface IIFillAndOpacity { | ||
fill: string | undefined; | ||
@@ -26,2 +27,15 @@ opacity: number | undefined; | ||
} | ||
interface IIVeryLongText { | ||
index: number; | ||
value: string | number; | ||
fitValue: string | number; | ||
restValue: string | number; | ||
fitHeight: number; | ||
} | ||
declare type ITVeryLongText = IIVeryLongText | null; | ||
interface IICalcRowHeight { | ||
height: number; | ||
haveLongText: boolean; | ||
veryLongText: ITVeryLongText[] | null; | ||
} | ||
interface IIPdfkitTableCache { | ||
@@ -58,3 +72,3 @@ title: string | ITitle; | ||
prepareRowFillOptionsData: (object: any) => IIFillAndOpacity; | ||
prepareCellPadding(p: number | number[]): IPadding; | ||
prepareCellPadding(p: IICellPaddingInput): IPadding; | ||
addPageAsync(): Promise<void>; | ||
@@ -70,4 +84,4 @@ pageAddedFire(): Promise<void>; | ||
createRowObject(data: any): Promise<void>; | ||
calcRowHeightString(row: any, opt: IICalcRowHeightOptions): Promise<number | any>; | ||
calcRowHeightObject(row: any, opt: IICalcRowHeightOptions): Promise<number | any[]>; | ||
calcRowHeightString(row: any, opt: IICalcRowHeightOptions): Promise<IICalcRowHeight>; | ||
calcRowHeightObject(row: any, opt: IICalcRowHeightOptions): Promise<IICalcRowHeight>; | ||
calcTitleSubtitleHeaderAndFirstLine(): Promise<number>; | ||
@@ -85,12 +99,4 @@ calcLimitCellOnPage(y: number, height: number): boolean; | ||
}; | ||
table(table: ITable, options: IOptions, callback?: Function): Promise<{ | ||
y: any; | ||
x: any; | ||
width: number; | ||
pages: number; | ||
lines: number; | ||
columns: number; | ||
summation: any[]; | ||
}>; | ||
tables(tables: ITable[], callback?: Function): Promise<unknown>; | ||
table(table: ITable, options: IOptions, callback?: Function): Promise<object>; | ||
tables(tables: ITable[]): Promise<unknown>; | ||
logg(...args: any[]): void; | ||
@@ -97,0 +103,0 @@ } |
105
lib/index.js
@@ -101,5 +101,6 @@ "use strict"; | ||
resetPeerTable() { | ||
const distance = this.pdfkitTableCache.distanceCorrection || 1; | ||
this.initialPositionX = this.pdfkitTableCache.options.x; | ||
this.positionX = this.pdfkitTableCache.options.x; | ||
this.positionY = (this.pdfkitTableCache.options.y || this.y) + (this.pdfkitTableCache.distanceCorrection * 2); | ||
this.positionY = (this.pdfkitTableCache.options.y || this.y) + (distance * 2); | ||
this.titleHeight = this.pdfkitTableCache.title ? 20 : (this.pdfkitTableCache.options.title ? 20 : 0); | ||
@@ -122,3 +123,3 @@ this.subtitleHeight = this.pdfkitTableCache.subtitle ? 15 : (this.pdfkitTableCache.options.subtitle ? 15 : 0); | ||
w = this.page.width - this.page.margins.right - (this.pdfkitTableCache.options.x || 0); | ||
this.pdfkitTableCache.options.width && (w = Number(String(this.pdfkitTableCache.options.width).replace(/\D+/g, ''))); | ||
this.pdfkitTableCache.options.width && (w = parseInt(String(this.pdfkitTableCache.options.width)) || Number(String(this.pdfkitTableCache.options.width).replace(/[^0-9.]/g, ''))); | ||
this.pdfkitTableCache.headers.forEach((el) => el.width && h.push(el.width >> 0)); | ||
@@ -244,6 +245,2 @@ h.length === 0 && (h = this.pdfkitTableCache.options.columnsSize || []); | ||
this.positionY = this.page.margins.top; | ||
if (this.doNotCreateHeader) { | ||
this.doNotCreateHeader = undefined; | ||
return; | ||
} | ||
}); | ||
@@ -257,3 +254,3 @@ } | ||
const { x, y, width, height } = rect; | ||
const distance = this.pdfkitTableCache.distanceCorrection; | ||
const distance = this.pdfkitTableCache.distanceCorrection || 1; | ||
fillColor || (fillColor = 'grey'); | ||
@@ -278,3 +275,3 @@ fillOpacity || (fillOpacity = 0.1); | ||
var _a, _b; | ||
const distance = this.pdfkitTableCache.distanceCorrection; | ||
const distance = this.pdfkitTableCache.distanceCorrection || 1; | ||
let direction; | ||
@@ -297,2 +294,4 @@ switch (type) { | ||
strokeOpacity = opacity || strokeOpacity || 0.5; | ||
x || (x = this.x); | ||
y || (y = this.y); | ||
if (disabled !== undefined) { | ||
@@ -304,3 +303,3 @@ strokeDisabled = disabled; | ||
} | ||
const s = (strokeWidth / 2) - distance; | ||
const s = parseFloat(Number(strokeWidth / 2).toFixed(4) || '0') - distance; | ||
if (strokeDisabled) { | ||
@@ -330,3 +329,2 @@ this.positionY += (distance * 2); | ||
if (this.calcLimitCellOnPage(0, this.titleAndHeaderAndFirstLineHeightCalc)) { | ||
this.doNotCreateHeader = true; | ||
yield this.addPageAsync(); | ||
@@ -350,2 +348,3 @@ } | ||
const { x, y } = this.pdfkitTableCache.options; | ||
const distance = this.pdfkitTableCache.distanceCorrection || 1; | ||
let titleHeight = 0; | ||
@@ -385,3 +384,3 @@ this.save(); | ||
this.opacity(1); | ||
this.positionY += titleHeight + (this.pdfkitTableCache.distanceCorrection * 2); | ||
this.positionY += titleHeight + (distance * 2); | ||
resolve(); | ||
@@ -395,3 +394,3 @@ }); | ||
try { | ||
const { top, right, left } = Object(this.pdfkitTableCache.options.padding); | ||
const { top, right, left } = this.pdfkitTableCache.options.padding; | ||
let colIndex; | ||
@@ -470,4 +469,4 @@ let colLen = this.pdfkitTableCache.table.columns || 0; | ||
} | ||
const { top, right, left } = Object(this.pdfkitTableCache.options.padding); | ||
const distance = this.pdfkitTableCache.distanceCorrection / 2; | ||
const { top, right, left } = this.pdfkitTableCache.options.padding; | ||
const distance = this.pdfkitTableCache.distanceCorrection || 1; | ||
let rowIndex = 0; | ||
@@ -483,3 +482,3 @@ let colIndex = 0; | ||
for (rowIndex = 0; rowIndex < rowLen; rowIndex++) { | ||
const { height, veryLongText, haveLongText } = Object(yield this.calcRowHeightString(rows[rowIndex], { isHeader: false, preventLongText: true })); | ||
const { height, veryLongText, haveLongText } = yield this.calcRowHeightString(rows[rowIndex], { isHeader: false, preventLongText: true }); | ||
this.rowHeight = Number(height); | ||
@@ -497,5 +496,5 @@ if (haveLongText) | ||
if (haveLongText) { | ||
let lt = veryLongText[colIndex]; | ||
let lt = veryLongText ? veryLongText[colIndex] : null; | ||
if (lt) { | ||
const { fitValue, fitHeight } = Object(lt); | ||
const { fitValue, fitHeight } = lt; | ||
text = fitValue; | ||
@@ -513,3 +512,3 @@ this.rowHeight = fitHeight; | ||
(_d = (_c = this.pdfkitTableCache.options) === null || _c === void 0 ? void 0 : _c.prepareRow) === null || _d === void 0 ? void 0 : _d.call(_c, elm, colIndex, rowIndex, rectRow, rectCell); | ||
this.text(text, this.columnPositions[colIndex] + left + padding.left, this.positionY + top + padding.top - distance, { | ||
this.text(text, this.columnPositions[colIndex] + left + padding.left, this.positionY + top + padding.top - (distance / 2), { | ||
width: this.columnSizes[colIndex] - (left + right) - (padding.left + padding.right), | ||
@@ -542,4 +541,4 @@ align: 'left', | ||
} | ||
const { top, right, bottom, left } = Object(this.pdfkitTableCache.options.padding); | ||
const distance = this.pdfkitTableCache.distanceCorrection / 2; | ||
const { top, right, bottom, left } = this.pdfkitTableCache.options.padding; | ||
const distance = this.pdfkitTableCache.distanceCorrection || 1; | ||
let rowIndex = 0; | ||
@@ -553,3 +552,3 @@ let colIndex = 0; | ||
for (rowIndex = 0; rowIndex < rowLen; rowIndex++) { | ||
const { height, veryLongText, haveLongText } = Object(yield this.calcRowHeightObject(datas[rowIndex], { isHeader: false, preventLongText: true })); | ||
const { height, veryLongText, haveLongText } = yield this.calcRowHeightObject(datas[rowIndex], { isHeader: false, preventLongText: true }); | ||
this.rowHeight = Number(height); | ||
@@ -569,5 +568,5 @@ if (haveLongText) | ||
if (haveLongText) { | ||
let lt = veryLongText[colIndex]; | ||
let lt = veryLongText ? veryLongText[colIndex] : null; | ||
if (lt) { | ||
const { fitValue, fitHeight } = Object(lt); | ||
const { fitValue, fitHeight } = lt; | ||
text = fitValue; | ||
@@ -609,3 +608,3 @@ this.rowHeight = fitHeight; | ||
(_d = (_c = this.pdfkitTableCache.options) === null || _c === void 0 ? void 0 : _c.prepareRow) === null || _d === void 0 ? void 0 : _d.call(_c, elm, colIndex, rowIndex, rectRow, rectCell); | ||
this.text(text, this.columnPositions[colIndex] + left + padding.left, this.positionY + top + padding.top - distance, { | ||
this.text(text, this.columnPositions[colIndex] + left + padding.left, this.positionY + top + padding.top - (distance / 2), { | ||
width: this.columnSizes[colIndex] - (left + right) - (padding.left + padding.right), | ||
@@ -626,3 +625,4 @@ align: 'left', | ||
let { align, isHeader, preventLongText } = opt; | ||
const { left, top, right, bottom } = Object(this.pdfkitTableCache.options.padding); | ||
const { left, top, right, bottom } = this.pdfkitTableCache.options.padding; | ||
const distance = this.pdfkitTableCache.distanceCorrection || 1; | ||
isHeader === undefined && (isHeader = false); | ||
@@ -657,3 +657,3 @@ align || (align = 'left'); | ||
haveLongText = true; | ||
let safeHeight = this.pdfkitTableCache.safelyPageHeight - this.headerHeight - (this.pdfkitTableCache.distanceCorrection * 4); | ||
let safeHeight = this.pdfkitTableCache.safelyPageHeight - this.headerHeight - (distance * 4); | ||
let percent = heightCompute / (safeHeight) + 0.01; | ||
@@ -685,3 +685,2 @@ let lenTextTest = text.length / percent - 50; | ||
index: colIndex, | ||
key: null, | ||
value: text, | ||
@@ -698,13 +697,18 @@ fitValue: `${fitValue} +${text.length - fitValueLength - 7}...`, | ||
} | ||
height = height + this.pdfkitTableCache.distanceCorrection + top + bottom; | ||
height = height + distance + top + bottom; | ||
if (preventLongText) { | ||
resolve({ | ||
height, | ||
haveLongText, | ||
veryLongText, | ||
haveLongText | ||
}); | ||
} | ||
else { | ||
resolve(height); | ||
resolve({ | ||
height, | ||
haveLongText: false, | ||
veryLongText: null, | ||
}); | ||
} | ||
return; | ||
})); | ||
@@ -717,3 +721,4 @@ } | ||
let { align, isHeader, preventLongText } = opt; | ||
const { left, top, right, bottom } = Object(this.pdfkitTableCache.options.padding); | ||
const { left, top, right, bottom } = this.pdfkitTableCache.options.padding; | ||
const distance = this.pdfkitTableCache.distanceCorrection || 1; | ||
isHeader === undefined && (isHeader = false); | ||
@@ -750,3 +755,3 @@ align || (align = 'left'); | ||
haveLongText = true; | ||
let safeHeight = this.pdfkitTableCache.safelyPageHeight - this.headerHeight - (this.pdfkitTableCache.distanceCorrection * 4); | ||
let safeHeight = this.pdfkitTableCache.safelyPageHeight - this.headerHeight - (distance * 4); | ||
let percent = heightCompute / (safeHeight) + 0.01; | ||
@@ -778,3 +783,2 @@ let fitHeight = safeHeight; | ||
index: i, | ||
key: null, | ||
value: text, | ||
@@ -791,12 +795,16 @@ fitValue: `${fitValue} +${text.length - fitValueLength - 7}...`, | ||
} | ||
height = height + this.pdfkitTableCache.distanceCorrection + (top + bottom); | ||
height = height + distance + (top + bottom); | ||
if (preventLongText) { | ||
resolve([ | ||
resolve({ | ||
height, | ||
haveLongText, | ||
veryLongText, | ||
haveLongText | ||
]); | ||
}); | ||
} | ||
else { | ||
resolve(height); | ||
resolve({ | ||
height, | ||
haveLongText: false, | ||
veryLongText: null, | ||
}); | ||
} | ||
@@ -811,8 +819,11 @@ return; | ||
const { headers, datas, rows } = this.pdfkitTableCache; | ||
const distance = this.pdfkitTableCache.distanceCorrection || 1; | ||
if (this.headerHeight === 0) { | ||
if (this.isHeaderString === false) { | ||
this.headerHeight = Number(yield this.calcRowHeightObject(headers, { isHeader: true })); | ||
const { height } = yield this.calcRowHeightObject(headers, { isHeader: true }); | ||
this.headerHeight = height; | ||
} | ||
else { | ||
this.headerHeight = Number(yield this.calcRowHeightString(headers, { isHeader: true })); | ||
const { height } = yield this.calcRowHeightString(headers, { isHeader: true }); | ||
this.headerHeight = height; | ||
} | ||
@@ -822,7 +833,9 @@ } | ||
if (datas.length > 0) { | ||
this.firstLineHeight = Number(yield this.calcRowHeightObject(datas[0], { isHeader: true })); | ||
const { height } = yield this.calcRowHeightObject(datas[0], { isHeader: true }); | ||
this.firstLineHeight = height; | ||
this.logg(this.firstLineHeight, 'datas'); | ||
} | ||
else if (rows.length > 0) { | ||
this.firstLineHeight = Number(yield this.calcRowHeightString(rows[0], { isHeader: true })); | ||
const { height } = yield this.calcRowHeightString(rows[0], { isHeader: true }); | ||
this.firstLineHeight = height; | ||
this.logg(this.firstLineHeight, 'rows'); | ||
@@ -835,3 +848,3 @@ } | ||
this.firstLineHeight + | ||
(this.pdfkitTableCache.distanceCorrection * 2)); | ||
(distance * 2)); | ||
resolve(calc); | ||
@@ -893,3 +906,3 @@ return; | ||
} | ||
tables(tables, callback) { | ||
tables(tables) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
@@ -899,3 +912,4 @@ return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () { | ||
if (Array.isArray(tables)) { | ||
for (let i = 0; i < tables.length; i++) { | ||
let len = tables.length; | ||
for (let i = 0; i < len; i++) { | ||
yield this.table(tables[i], tables[i].options || {}); | ||
@@ -907,3 +921,2 @@ } | ||
} | ||
typeof callback === 'function' && callback(this.tableResume()); | ||
resolve(this.tableResume()); | ||
@@ -910,0 +923,0 @@ } |
{ | ||
"name": "pdfkit-table-ts", | ||
"version": "0.0.13", | ||
"description": "PdfKit Table. Helps to draw informations in simple tables using pdfkit. #server-side. Generate pdf tables with javascript (PDFKIT plugin) ", | ||
"version": "0.0.14", | ||
"description": "PdfKit Table. Helps to draw informatins in simple tables using pdfkit. #server-side. Generate pdf tables with javascript (PDFKIT plugin) ", | ||
"main": "lib/index.js", | ||
@@ -6,0 +6,0 @@ "types": "lib/types.d.ts", |
@@ -27,17 +27,2 @@ # pdfkit-table-ts | ||
["England", "33%", "+4.44%"], | ||
["England of England of England", "33%", "+4.44%"], | ||
["Switzerland", "12%", "+1.12%"], | ||
["France", "67%", "-0.98%"], | ||
["England", "33%", "+4.44%"], | ||
["England of England of England", "33%", "+4.44%"], | ||
["Switzerland", "12%", "+1.12%"], | ||
["France", "67%", "-0.98%"], | ||
["England", "33%", "+4.44%"], | ||
["England of England of England", "33%", "+4.44%"], | ||
["Switzerland", "12%", "+1.12%"], | ||
["France", "67%", "-0.98%"], | ||
["England", "33%", "+4.44%"], | ||
["England of England of England", "33%", "+4.44%"], | ||
["Switzerland", "12%", "+1.12%"], | ||
["France", "67%", "-0.98%"], | ||
], | ||
@@ -44,0 +29,0 @@ }; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
106432
1182
69