diff --git a/index.d.ts b/index.d.ts index ab0684d..d43cc71 100644 --- a/index.d.ts +++ b/index.d.ts @@ -92,6 +92,8 @@ declare module 'pdfkit-table' class PDFDocumentWithTables extends PDFDocument { public table(table: Table, options?: Options): Promise; + public queueRenderOnAddPage(section: (doc: PDFDocumentWithTables) => void) + public addBackground(rect: Rect, fillColor: string, fillOpacity: number); } // export = PDFDocumentWithTables; diff --git a/index.js b/index.js index 6344638..a781105 100644 --- a/index.js +++ b/index.js @@ -6,13 +6,25 @@ const PDFDocument = require("pdfkit"); // const EventEmitter = require('events').EventEmitter; class PDFDocumentWithTables extends PDFDocument { + rendersOnAddPage; constructor(option) { super(option); this.opt = option; + this.rendersOnAddPage = [] // this.emitter = new EventEmitter(); } + /** + * queueRenderOnAddPage + * @param {(doc: PDFDocumentWithTables) => void} section + */ + queueRenderOnAddPage(section, callback) { + this.rendersOnAddPage.push(section) + typeof callback === 'function' && callback(this); + } + + logg(...args) { // console.log(args); } @@ -196,6 +208,7 @@ class PDFDocumentWithTables extends PDFDocument { size: this.page.size, margins: this.page.margins, }); + this.rendersOnAddPage.forEach(section => section(this)) lockAddHeader || addHeader(); //addHeader(); }; @@ -483,7 +496,7 @@ class PDFDocumentWithTables extends PDFDocument { this.logg('CRAZY! This a big text on cell'); } else if(calc > maxY) { // && !lockAddPage // lockAddHeader = false; - lockAddPage = true; + lockAddPage = false; onFirePageAdded(); // this.emitter.emit('addPage'); //this.addPage(); return; } @@ -657,6 +670,7 @@ class PDFDocumentWithTables extends PDFDocument { // For safety, consider 3 rows margin instead of just one // if (startY + 2 * rowHeight < maxY) startY = rowBottomY + columnSpacing + rowDistance; // 0.5 is spacing rows // else this.emitter.emit('addPage'); //this.addPage(); + console.log('plugin datas', this.y, this.y + safelyMarginBottom + rowHeight) if(options.useSafelyMarginBottom && this.y + safelyMarginBottom + rowHeight >= maxY && !lockAddPage) onFirePageAdded(); // this.emitter.emit('addPage'); //this.addPage(); // calc position