Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix multiline text not preserved when using "maxWidth" property #2942

Merged
merged 1 commit into from Oct 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/jspdf.js
Expand Up @@ -3531,7 +3531,9 @@ function jsPDF(options) {
if (typeof text === "string") {
text = scope.splitTextToSize(text, maxWidth);
} else if (Object.prototype.toString.call(text) === "[object Array]") {
text = scope.splitTextToSize(text.join(" "), maxWidth);
text = text.reduce(function(acc, textLine) {
return acc.concat(scope.splitTextToSize(textLine, maxWidth));
}, []);
}
}

Expand Down
17 changes: 5 additions & 12 deletions src/modules/cell.js
Expand Up @@ -202,7 +202,9 @@ import { jsPDF } from "../jspdf.js";
if (typeof text === "string") {
text = this.splitTextToSize(text, maxWidth);
} else if (Object.prototype.toString.call(text) === "[object Array]") {
text = this.splitTextToSize(text.join(" "), maxWidth);
text = text.reduce(function(acc, textLine) {
return acc.concat(scope.splitTextToSize(textLine, maxWidth));
}, []);
}
} else {
// Without the else clause, it will not work if you do not pass along maxWidth
Expand Down Expand Up @@ -558,18 +560,9 @@ import { jsPDF } from "../jspdf.js";

return Object.keys(model)
.map(function(key) {
return [key, model[key]];
listopadiya marked this conversation as resolved.
Show resolved Hide resolved
})
.map(function(item) {
var key = item[0];
var value = item[1];
return typeof value === "object" ? [key, value.text] : [key, value];
})
.map(function(item) {
var key = item[0];
var value = item[1];
var value = model[key];
return this.splitTextToSize(
value,
value.hasOwnProperty("text") ? value.text : value,
columnWidths[key] - padding - padding
);
}, this)
Expand Down
6 changes: 6 additions & 0 deletions test/specs/cell.spec.js
Expand Up @@ -26,6 +26,12 @@ describe("Module: Cell", () => {
expect(
doc.getTextDimensions("Octocat loves jsPDF", { maxWidth: 100 }).w
).toEqual(96.64000000000001);
expect(
doc.getTextDimensions("Octocat loves jsPDF\njsPDF loves Octocat", { maxWidth: 100 }).h
).toEqual(71.19999999999999);
expect(
doc.getTextDimensions("Octocat loves jsPDF\njsPDF loves Octocat", { maxWidth: 100 }).w
).toEqual(96.64000000000001);
expect(doc.getTextDimensions("").w).toEqual(0);
expect(doc.getTextDimensions("").h).toEqual(0);
expect(doc.getTextDimensions([""]).w).toEqual(0);
Expand Down
29 changes: 29 additions & 0 deletions test/specs/jspdf.unit.spec.js
Expand Up @@ -2491,6 +2491,35 @@ break`,
"ET"
].join("\n")
]);

//check for maxWidth-functionality - has to split, preserving multiline;
doc = jsPDF({ floatPrecision: 2 });
writeArray = [];
doc.__private__.setCustomOutputDestination(writeArray);
doc.__private__.text(
`This is a test.
This is a test too.`,
10,
10,
{
scope: doc,
maxWidth: 30
}
);
expect(writeArray).toEqual([
[
"BT",
"/F1 16 Tf",
"18.4 TL",
"0 g",
"28.35 813.54 Td",
"(This is a) Tj",
"T* (test.) Tj",
"T* (This is a) Tj",
"T* (test too.) Tj",
"ET"
].join("\n")
]);
});

it("jsPDF private function setLineCap", () => {
Expand Down