diff --git a/src/modules/cell.js b/src/modules/cell.js index fce190739..6ddaafeb1 100644 --- a/src/modules/cell.js +++ b/src/modules/cell.js @@ -369,6 +369,9 @@ import { jsPDF } from "../jspdf.js"; * @param {Object} [config.fontSize] Integer fontSize to use (optional) * @param {Object} [config.padding] cell-padding in pt to use (optional) * @param {Object} [config.headerBackgroundColor] default is #c8c8c8 (optional) + * @param {Object} [config.headerTextColor] default is #000 (optional) + * @param {Object} [config.rowStart] callback to handle before print each row (optional) + * @param {Object} [config.cellStart] callback to handle before print each cell (optional) * @returns {jsPDF} jsPDF-instance */ @@ -401,7 +404,8 @@ import { jsPDF } from "../jspdf.js"; config.margins || Object.assign({ width: this.getPageWidth() }, NO_MARGINS), padding = typeof config.padding === "number" ? config.padding : 3, - headerBackgroundColor = config.headerBackgroundColor || "#c8c8c8"; + headerBackgroundColor = config.headerBackgroundColor || "#c8c8c8", + headerTextColor = config.headerTextColor || "#000"; _reset.call(this); @@ -410,6 +414,7 @@ import { jsPDF } from "../jspdf.js"; this.internal.__cell__.table_font_size = fontSize; this.internal.__cell__.padding = padding; this.internal.__cell__.headerBackgroundColor = headerBackgroundColor; + this.internal.__cell__.headerTextColor = headerTextColor; this.setFontSize(fontSize); // Set header values @@ -525,9 +530,29 @@ import { jsPDF } from "../jspdf.js"; return pv; }, {}); for (i = 0; i < data.length; i += 1) { + if ("rowStart" in config && config.rowStart instanceof Function) { + config.rowStart( + { + row: i, + data: data[i] + }, + this + ); + } var lineHeight = calculateLineHeight.call(this, data[i], columnWidths); for (j = 0; j < headerNames.length; j += 1) { + var cellData = data[i][headerNames[j]]; + if ("cellStart" in config && config.cellStart instanceof Function) { + config.cellStart( + { + row: i, + col: j, + data: cellData + }, + this + ); + } cell.call( this, new Cell( @@ -535,7 +560,7 @@ import { jsPDF } from "../jspdf.js"; y, columnWidths[headerNames[j]], lineHeight, - data[i][headerNames[j]], + cellData, i + 2, align[headerNames[j]] ) @@ -637,8 +662,11 @@ import { jsPDF } from "../jspdf.js"; tempHeaderConf.push(tableHeaderCell); } tableHeaderCell.lineNumber = lineNumber; + var currentTextColor = this.getTextColor(); + this.setTextColor(this.internal.__cell__.headerTextColor); this.setFillColor(this.internal.__cell__.headerBackgroundColor); cell.call(this, tableHeaderCell); + this.setTextColor(currentTextColor); } if (tempHeaderConf.length > 0) { this.setTableHeaderRow(tempHeaderConf); diff --git a/test/reference/table-autoSize-headerNames.pdf b/test/reference/table-autoSize-headerNames.pdf index 9ec04ddbd..ac9fc892b 100644 Binary files a/test/reference/table-autoSize-headerNames.pdf and b/test/reference/table-autoSize-headerNames.pdf differ diff --git a/test/reference/table-autoSize.pdf b/test/reference/table-autoSize.pdf index d49b200f4..988386b1c 100644 Binary files a/test/reference/table-autoSize.pdf and b/test/reference/table-autoSize.pdf differ diff --git a/test/reference/table-formatted.pdf b/test/reference/table-formatted.pdf new file mode 100644 index 000000000..81e098aa3 Binary files /dev/null and b/test/reference/table-formatted.pdf differ diff --git a/test/reference/table.pdf b/test/reference/table.pdf index 40d0f2305..d0b19ec8a 100644 Binary files a/test/reference/table.pdf and b/test/reference/table.pdf differ diff --git a/test/specs/cell.spec.js b/test/specs/cell.spec.js index 918da94fc..19b1a85c1 100644 --- a/test/specs/cell.spec.js +++ b/test/specs/cell.spec.js @@ -122,6 +122,31 @@ describe("Module: Cell", () => { comparePdf(doc.output(), "table-autoSize.pdf"); }); + it("table-formatted", () => { + var doc = new jsPDF({ + putOnlyUsedFonts: true, + orientation: "landscape", + floatPrecision: 2 + }); + doc.table(1, 1, generateData(100), header, { + rowStart: function(e, docInstance) { + // docInstance equal to doc + if (17 < e.row && e.row < 36) + docInstance.setTextColor(255,0,0); + else + docInstance.setTextColor(0,0,0); + }, + cellStart: function(e, docInstance) { + // docInstance equal to doc + if (e.row === 27 && e.col === 3) + docInstance.setFont(undefined, "bold"); + else + docInstance.setFont(undefined, "normal"); + } + }); + comparePdf(doc.output(), "table-formatted.pdf"); + }); + it("table error handling", () => { var doc = new jsPDF({ putOnlyUsedFonts: true, orientation: "landscape" }); expect(function() { diff --git a/types/index.d.ts b/types/index.d.ts index d49d65811..516c94003 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -570,6 +570,17 @@ declare module "jspdf" { y: number; } + export interface TableRowData { + row?: number; + data?: any[]; + } + + export interface TableCellData { + row?: number; + col?: number; + data?: any; + } + export interface TableConfig { printHeaders?: boolean; autoSize?: boolean; @@ -577,6 +588,9 @@ declare module "jspdf" { fontSize?: number; padding?: number; headerBackgroundColor?: string; + headerTextColor?: string; + rowStart?: (e: TableRowData, doc: jsPDF) => void; + cellStart?: (e: TableCellData, doc: jsPDF) => void; css?: { "font-size": number; };