Skip to content

Commit

Permalink
table method: add event for start printing row and cell (#3039)
Browse files Browse the repository at this point in the history
Co-authored-by: Lukas Holländer <[email protected]>
  • Loading branch information
mojtaba-khallash and HackbrettXXX authored Sep 10, 2021
1 parent 18c56bf commit 574c6f0
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 2 deletions.
32 changes: 30 additions & 2 deletions src/modules/cell.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/

Expand Down Expand Up @@ -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);

Expand All @@ -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
Expand Down Expand Up @@ -525,17 +530,37 @@ 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(
x,
y,
columnWidths[headerNames[j]],
lineHeight,
data[i][headerNames[j]],
cellData,
i + 2,
align[headerNames[j]]
)
Expand Down Expand Up @@ -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);
Expand Down
Binary file modified test/reference/table-autoSize-headerNames.pdf
Binary file not shown.
Binary file modified test/reference/table-autoSize.pdf
Binary file not shown.
Binary file added test/reference/table-formatted.pdf
Binary file not shown.
Binary file modified test/reference/table.pdf
Binary file not shown.
25 changes: 25 additions & 0 deletions test/specs/cell.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
14 changes: 14 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -570,13 +570,27 @@ 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;
margins?: number;
fontSize?: number;
padding?: number;
headerBackgroundColor?: string;
headerTextColor?: string;
rowStart?: (e: TableRowData, doc: jsPDF) => void;
cellStart?: (e: TableCellData, doc: jsPDF) => void;
css?: {
"font-size": number;
};
Expand Down

0 comments on commit 574c6f0

Please sign in to comment.