From 95843f921ec977d0de1b27851a16d17721e9a956 Mon Sep 17 00:00:00 2001 From: Bao Dang Date: Wed, 4 Sep 2024 10:08:47 -0400 Subject: [PATCH] Add border radius customization for rects and header boxes --- index.d.ts | 2 ++ index.js | 50 ++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/index.d.ts b/index.d.ts index ab0684d..9a3b8e1 100644 --- a/index.d.ts +++ b/index.d.ts @@ -7,6 +7,7 @@ declare module 'pdfkit-table' y: number; width: number; height: number; + round: number; } interface Header { @@ -17,6 +18,7 @@ declare module 'pdfkit-table' valign?: string; headerColor?: string; //default '#BEBEBE' headerOpacity?: number; //default '0.5' + headerRound?: number; // default '0' headerAlign?: string; //default 'left' columnColor?: string; columnOpacity?: number; diff --git a/index.js b/index.js index 6344638..20229f7 100644 --- a/index.js +++ b/index.js @@ -24,7 +24,7 @@ class PDFDocumentWithTables extends PDFDocument { * @param {Number} fillOpacity * @param {Function} callback */ - addBackground ({x, y, width, height}, fillColor, fillOpacity, callback) { + addBackground ({x, y, width, height, round}, fillColor, fillOpacity, callback) { // validate fillColor || (fillColor = 'grey'); @@ -34,13 +34,29 @@ class PDFDocumentWithTables extends PDFDocument { this.save(); // draw bg - this - .fill(fillColor) - //.stroke(fillColor) - .fillOpacity(fillOpacity) - .rect( x, y, width, height ) - //.stroke() - .fill(); + if (round) { + this + .fill(fillColor) + //.stroke(fillColor) + .fillOpacity(fillOpacity) + .roundedRect( x, y, width, height, Math.abs(round) ) + .rect( x, y + height / 2, width, height / 2 ); + if (round > 0) { + this.rect( x + width / 2, y, width / 2, height); + } else { + this.rect( x, y, width / 2, height); + } + //.stroke() + this.fill(); + } else { + this + .fill(fillColor) + //.stroke(fillColor) + .fillOpacity(fillOpacity) + .rect( x, y, width, height ) + //.stroke() + .fill(); + } // back to saved style this.restore(); @@ -132,7 +148,7 @@ class PDFDocumentWithTables extends PDFDocument { let lockAddTitles = false; // to addd title one time let lockAddPage = false; let lockAddHeader = false; - let safelyMarginBottom = this.page.margins.top/2; + let safelyMarginBottom = this.page.margins.bottom; // reset position to margins.left if( options.x === null || options.x === -1 ){ @@ -350,7 +366,7 @@ class PDFDocumentWithTables extends PDFDocument { // define row with properties header row = cells; } - + row.forEach((cell,i) => { let text = cell; @@ -477,13 +493,14 @@ class PDFDocumentWithTables extends PDFDocument { const calc = startY + titleHeight + firstLineHeight + this.headerHeight + safelyMarginBottom// * 1.3; // content is big text (crazy!) + // console.log("pdfkit-table", calc, maxY, this.y, this.page.height, this.page.margins.bottom) if(firstLineHeight > maxY) { // lockAddHeader = true; lockAddPage = true; 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; } @@ -568,10 +585,11 @@ class PDFDocumentWithTables extends PDFDocument { // Print all headers table.headers.forEach( (dataHeader, i) => { - let {label, width, renderer, align, headerColor, headerOpacity, headerAlign, padding} = dataHeader; + let {label, width, renderer, align, valign, headerColor, headerOpacity, headerAlign, headerValign, headerRound, padding} = dataHeader; // check defination width = width || columnSizes[i]; align = headerAlign || align || 'left'; + valign = headerValign || valign || 'top'; // force number width = width >> 0; @@ -607,15 +625,19 @@ class PDFDocumentWithTables extends PDFDocument { }; // add background - this.addBackground(rectCell, headerColor, headerOpacity); + this.addBackground({...rectCell, round: headerRound}, headerColor, headerOpacity); // cell padding cellPadding = prepareCellPadding(padding || options.padding || 0); // write + const calcY = startY + (this.headerHeight - this.heightOfString(label, { + width: width - (cellPadding.left + cellPadding.right), + align: align, + })) / 2; this.text(label, lastPositionX + (cellPadding.left), - startY, { + valign ? calcY : startY, { width: width - (cellPadding.left + cellPadding.right), align: align, })