From c91995de97c598deaf6fda7109ea886a50f50109 Mon Sep 17 00:00:00 2001 From: Thomas Sablik Date: Wed, 10 Feb 2021 10:41:10 +0100 Subject: [PATCH] Fix: Wrong TypeScript type in jsPDF.table (#3086) (#3087) * also implicitly calculate cell widths if headers are passed as string array --- src/modules/cell.js | 2 +- test/reference/table-autoSize-headerNames.pdf | Bin 0 -> 57579 bytes test/specs/cell.spec.js | 28 ++++++++++++++++-- types/index.d.ts | 18 +++++++++-- 4 files changed, 41 insertions(+), 7 deletions(-) create mode 100644 test/reference/table-autoSize-headerNames.pdf diff --git a/src/modules/cell.js b/src/modules/cell.js index 01e59f833..1c69dbaa8 100644 --- a/src/modules/cell.js +++ b/src/modules/cell.js @@ -442,7 +442,7 @@ import { jsPDF } from "../jspdf.js"; }); } - if (autoSize) { + if (autoSize || (Array.isArray(headers) && typeof headers[0] === "string")) { var headerName; for (i = 0; i < headerNames.length; i += 1) { headerName = headerNames[i]; diff --git a/test/reference/table-autoSize-headerNames.pdf b/test/reference/table-autoSize-headerNames.pdf new file mode 100644 index 0000000000000000000000000000000000000000..9ec04ddbd0cdf92a6998b27b36630276c87b9277 GIT binary patch literal 57579 zcmeI5+m74F5r*&i6udFkMz9pmqyl3BpT@h00~_`LS-@BX+Oa%4##k~m8t-nNDNm4> zNggFdR*_ZggFMxU11!+K7<**RM~U@KceDFf^WLkcXWtfCmE8N+zyJB4q)PL2|MQ3B z;ltJW@1M5m>ec3Qo50t@c6Uq*_}|yb>h<(^ zG0C&0OMgxB^f~;q>(k4m%=!w@WmY#S9H%YPZ>v1(i}bKf9w(2_lhs+77G-+=J}IiK zPtRY#PhTblyu~_gx+?4H^!#0N|6+f&dyt-gNS>Wf=(^5ocn-4rKI^)>v70hmHt}h?nY6m~auYWwo4i01-qAb=~Sx$#ocUif<+q)>s zF6-b2jYE9i9&WDo=BcWRHf!cL(yoUB+^G&#*$i)59OdKY;+LzP`8KM)&8qnTP~mlT z#{;bEtY5s1&)3K49HBfdXj5DAyg_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spec.js b/test/specs/cell.spec.js index aa13b31b3..918da94fc 100644 --- a/test/specs/cell.spec.js +++ b/test/specs/cell.spec.js @@ -71,16 +71,18 @@ describe("Module: Cell", () => { })); } - var header = createHeaders([ + var headerNames = [ "coin", "game_group", "game_name", "game_version", "machine", "vlt" - ]); + ]; - it("table", () => { + var header = createHeaders(headerNames); + + it("table with CellConfig[]", () => { var doc = new jsPDF({ putOnlyUsedFonts: true, orientation: "landscape", @@ -90,6 +92,26 @@ describe("Module: Cell", () => { comparePdf(doc.output(), "table.pdf"); }); + it("table with string[] and without autoSize", () => { + var doc = new jsPDF({ + putOnlyUsedFonts: true, + orientation: "landscape", + floatPrecision: 2 + }); + doc.table(1, 1, generateData(100), headerNames); + comparePdf(doc.output(), "table-autoSize-headerNames.pdf"); + }); + + it("table with string[] and autoSize", () => { + var doc = new jsPDF({ + putOnlyUsedFonts: true, + orientation: "landscape", + floatPrecision: 2 + }); + doc.table(1, 1, generateData(100), headerNames, { autoSize: true }); + comparePdf(doc.output(), "table-autoSize-headerNames.pdf"); + }); + it("table-autoSize", () => { var doc = new jsPDF({ putOnlyUsedFonts: true, diff --git a/types/index.d.ts b/types/index.d.ts index 4a353a76a..638d6744b 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -563,6 +563,18 @@ declare module "jspdf" { y: number; } + export interface TableConfig { + printHeaders?: boolean; + autoSize?: boolean; + margins?: number; + fontSize?: number; + padding?: number; + headerBackgroundColor?: string; + css?: { + "font-size": number; + }; + } + export interface CellConfig { name: string; prompt: string; @@ -981,9 +993,9 @@ declare module "jspdf" { table( x: number, y: number, - data: any, - headers: string[], - config: any + data: { [key: string]: string }[], + headers: string[] | CellConfig[], + config: TableConfig ): jsPDF; calculateLineHeight( headerNames: string[],