Skip to content

Commit 574c6f0

Browse files
table method: add event for start printing row and cell (#3039)
Co-authored-by: Lukas Holländer <[email protected]>
1 parent 18c56bf commit 574c6f0

File tree

7 files changed

+69
-2
lines changed

7 files changed

+69
-2
lines changed

src/modules/cell.js

+30-2
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,9 @@ import { jsPDF } from "../jspdf.js";
369369
* @param {Object} [config.fontSize] Integer fontSize to use (optional)
370370
* @param {Object} [config.padding] cell-padding in pt to use (optional)
371371
* @param {Object} [config.headerBackgroundColor] default is #c8c8c8 (optional)
372+
* @param {Object} [config.headerTextColor] default is #000 (optional)
373+
* @param {Object} [config.rowStart] callback to handle before print each row (optional)
374+
* @param {Object} [config.cellStart] callback to handle before print each cell (optional)
372375
* @returns {jsPDF} jsPDF-instance
373376
*/
374377

@@ -401,7 +404,8 @@ import { jsPDF } from "../jspdf.js";
401404
config.margins ||
402405
Object.assign({ width: this.getPageWidth() }, NO_MARGINS),
403406
padding = typeof config.padding === "number" ? config.padding : 3,
404-
headerBackgroundColor = config.headerBackgroundColor || "#c8c8c8";
407+
headerBackgroundColor = config.headerBackgroundColor || "#c8c8c8",
408+
headerTextColor = config.headerTextColor || "#000";
405409

406410
_reset.call(this);
407411

@@ -410,6 +414,7 @@ import { jsPDF } from "../jspdf.js";
410414
this.internal.__cell__.table_font_size = fontSize;
411415
this.internal.__cell__.padding = padding;
412416
this.internal.__cell__.headerBackgroundColor = headerBackgroundColor;
417+
this.internal.__cell__.headerTextColor = headerTextColor;
413418
this.setFontSize(fontSize);
414419

415420
// Set header values
@@ -525,17 +530,37 @@ import { jsPDF } from "../jspdf.js";
525530
return pv;
526531
}, {});
527532
for (i = 0; i < data.length; i += 1) {
533+
if ("rowStart" in config && config.rowStart instanceof Function) {
534+
config.rowStart(
535+
{
536+
row: i,
537+
data: data[i]
538+
},
539+
this
540+
);
541+
}
528542
var lineHeight = calculateLineHeight.call(this, data[i], columnWidths);
529543

530544
for (j = 0; j < headerNames.length; j += 1) {
545+
var cellData = data[i][headerNames[j]];
546+
if ("cellStart" in config && config.cellStart instanceof Function) {
547+
config.cellStart(
548+
{
549+
row: i,
550+
col: j,
551+
data: cellData
552+
},
553+
this
554+
);
555+
}
531556
cell.call(
532557
this,
533558
new Cell(
534559
x,
535560
y,
536561
columnWidths[headerNames[j]],
537562
lineHeight,
538-
data[i][headerNames[j]],
563+
cellData,
539564
i + 2,
540565
align[headerNames[j]]
541566
)
@@ -637,8 +662,11 @@ import { jsPDF } from "../jspdf.js";
637662
tempHeaderConf.push(tableHeaderCell);
638663
}
639664
tableHeaderCell.lineNumber = lineNumber;
665+
var currentTextColor = this.getTextColor();
666+
this.setTextColor(this.internal.__cell__.headerTextColor);
640667
this.setFillColor(this.internal.__cell__.headerBackgroundColor);
641668
cell.call(this, tableHeaderCell);
669+
this.setTextColor(currentTextColor);
642670
}
643671
if (tempHeaderConf.length > 0) {
644672
this.setTableHeaderRow(tempHeaderConf);
641 Bytes
Binary file not shown.

test/reference/table-autoSize.pdf

641 Bytes
Binary file not shown.

test/reference/table-formatted.pdf

58.1 KB
Binary file not shown.

test/reference/table.pdf

636 Bytes
Binary file not shown.

test/specs/cell.spec.js

+25
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,31 @@ describe("Module: Cell", () => {
122122
comparePdf(doc.output(), "table-autoSize.pdf");
123123
});
124124

125+
it("table-formatted", () => {
126+
var doc = new jsPDF({
127+
putOnlyUsedFonts: true,
128+
orientation: "landscape",
129+
floatPrecision: 2
130+
});
131+
doc.table(1, 1, generateData(100), header, {
132+
rowStart: function(e, docInstance) {
133+
// docInstance equal to doc
134+
if (17 < e.row && e.row < 36)
135+
docInstance.setTextColor(255,0,0);
136+
else
137+
docInstance.setTextColor(0,0,0);
138+
},
139+
cellStart: function(e, docInstance) {
140+
// docInstance equal to doc
141+
if (e.row === 27 && e.col === 3)
142+
docInstance.setFont(undefined, "bold");
143+
else
144+
docInstance.setFont(undefined, "normal");
145+
}
146+
});
147+
comparePdf(doc.output(), "table-formatted.pdf");
148+
});
149+
125150
it("table error handling", () => {
126151
var doc = new jsPDF({ putOnlyUsedFonts: true, orientation: "landscape" });
127152
expect(function() {

types/index.d.ts

+14
Original file line numberDiff line numberDiff line change
@@ -570,13 +570,27 @@ declare module "jspdf" {
570570
y: number;
571571
}
572572

573+
export interface TableRowData {
574+
row?: number;
575+
data?: any[];
576+
}
577+
578+
export interface TableCellData {
579+
row?: number;
580+
col?: number;
581+
data?: any;
582+
}
583+
573584
export interface TableConfig {
574585
printHeaders?: boolean;
575586
autoSize?: boolean;
576587
margins?: number;
577588
fontSize?: number;
578589
padding?: number;
579590
headerBackgroundColor?: string;
591+
headerTextColor?: string;
592+
rowStart?: (e: TableRowData, doc: jsPDF) => void;
593+
cellStart?: (e: TableCellData, doc: jsPDF) => void;
580594
css?: {
581595
"font-size": number;
582596
};

0 commit comments

Comments
 (0)