Skip to content

Commit

Permalink
Merge pull request #2942 from listopadiya/master
Browse files Browse the repository at this point in the history
Fix multiline text not preserved when using "maxWidth" property
  • Loading branch information
HackbrettXXX committed Oct 5, 2020
2 parents 9b06098 + 4ccec9b commit 704f5a9
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 13 deletions.
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]];
})
.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

0 comments on commit 704f5a9

Please sign in to comment.