Skip to content

Commit 078d0fb

Browse files
authored
Merge pull request #2872 from skchawala/allow_number_in_table_cell
Fix-splitTextToSize method crashes if cell value is not a string #2849
2 parents ab21994 + 02c7faa commit 078d0fb

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

src/modules/cell.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,13 @@ import { jsPDF } from "../jspdf.js";
188188
var tempWidth = 0;
189189

190190
if (!Array.isArray(text) && typeof text !== "string") {
191-
throw new Error(
192-
"getTextDimensions expects text-parameter to be of type String or an Array of Strings."
193-
);
191+
if (typeof text === "number") {
192+
text = String(text);
193+
} else {
194+
throw new Error(
195+
"getTextDimensions expects text-parameter to be of type String or type Number or an Array of Strings."
196+
);
197+
}
194198
}
195199

196200
const maxWidth = options.maxWidth;

src/modules/split_text_to_size.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ import { jsPDF } from "../jspdf.js";
357357
if (Array.isArray(text)) {
358358
paragraphs = text;
359359
} else {
360-
paragraphs = text.split(/\r?\n/);
360+
paragraphs = String(text).split(/\r?\n/);
361361
}
362362

363363
// now we convert size (max length of line) into "font size units"

test/specs/cell.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ describe("Module: Cell", () => {
3434
doc.getTextDimensions();
3535
}).toThrow(
3636
new Error(
37-
"getTextDimensions expects text-parameter to be of type String or an Array of Strings."
37+
"getTextDimensions expects text-parameter to be of type String or type Number or an Array of Strings."
3838
)
3939
);
4040
});

0 commit comments

Comments
 (0)