Skip to content

Commit c91995d

Browse files
authored
Fix: Wrong TypeScript type in jsPDF.table (#3086) (#3087)
* also implicitly calculate cell widths if headers are passed as string array
1 parent 3592fc2 commit c91995d

File tree

4 files changed

+41
-7
lines changed

4 files changed

+41
-7
lines changed

src/modules/cell.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ import { jsPDF } from "../jspdf.js";
442442
});
443443
}
444444

445-
if (autoSize) {
445+
if (autoSize || (Array.isArray(headers) && typeof headers[0] === "string")) {
446446
var headerName;
447447
for (i = 0; i < headerNames.length; i += 1) {
448448
headerName = headerNames[i];
56.2 KB
Binary file not shown.

test/specs/cell.spec.js

+25-3
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,18 @@ describe("Module: Cell", () => {
7171
}));
7272
}
7373

74-
var header = createHeaders([
74+
var headerNames = [
7575
"coin",
7676
"game_group",
7777
"game_name",
7878
"game_version",
7979
"machine",
8080
"vlt"
81-
]);
81+
];
8282

83-
it("table", () => {
83+
var header = createHeaders(headerNames);
84+
85+
it("table with CellConfig[]", () => {
8486
var doc = new jsPDF({
8587
putOnlyUsedFonts: true,
8688
orientation: "landscape",
@@ -90,6 +92,26 @@ describe("Module: Cell", () => {
9092
comparePdf(doc.output(), "table.pdf");
9193
});
9294

95+
it("table with string[] and without autoSize", () => {
96+
var doc = new jsPDF({
97+
putOnlyUsedFonts: true,
98+
orientation: "landscape",
99+
floatPrecision: 2
100+
});
101+
doc.table(1, 1, generateData(100), headerNames);
102+
comparePdf(doc.output(), "table-autoSize-headerNames.pdf");
103+
});
104+
105+
it("table with string[] and autoSize", () => {
106+
var doc = new jsPDF({
107+
putOnlyUsedFonts: true,
108+
orientation: "landscape",
109+
floatPrecision: 2
110+
});
111+
doc.table(1, 1, generateData(100), headerNames, { autoSize: true });
112+
comparePdf(doc.output(), "table-autoSize-headerNames.pdf");
113+
});
114+
93115
it("table-autoSize", () => {
94116
var doc = new jsPDF({
95117
putOnlyUsedFonts: true,

types/index.d.ts

+15-3
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,18 @@ declare module "jspdf" {
563563
y: number;
564564
}
565565

566+
export interface TableConfig {
567+
printHeaders?: boolean;
568+
autoSize?: boolean;
569+
margins?: number;
570+
fontSize?: number;
571+
padding?: number;
572+
headerBackgroundColor?: string;
573+
css?: {
574+
"font-size": number;
575+
};
576+
}
577+
566578
export interface CellConfig {
567579
name: string;
568580
prompt: string;
@@ -981,9 +993,9 @@ declare module "jspdf" {
981993
table(
982994
x: number,
983995
y: number,
984-
data: any,
985-
headers: string[],
986-
config: any
996+
data: { [key: string]: string }[],
997+
headers: string[] | CellConfig[],
998+
config: TableConfig
987999
): jsPDF;
9881000
calculateLineHeight(
9891001
headerNames: string[],

0 commit comments

Comments
 (0)