Skip to content

Fix invalid string length when saving large PDF #3646

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 44 additions & 13 deletions src/jspdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -784,15 +784,6 @@ function jsPDF(options) {
);
});

var getArrayBuffer = (API.__private__.getArrayBuffer = function(data) {
var len = data.length,
ab = new ArrayBuffer(len),
u8 = new Uint8Array(ab);

while (len--) u8[len] = data.charCodeAt(len);
return ab;
});

var standardFonts = [
["Helvetica", "helvetica", "normal", "WinAnsiEncoding"],
["Helvetica-Bold", "helvetica", "bold", "WinAnsiEncoding"],
Expand Down Expand Up @@ -2998,11 +2989,51 @@ function jsPDF(options) {

setOutputDestination(pages[currentPage]);

return content;
});

var getString = (API.__private__.getString = function(content) {
return content.join("\n");
});

var getBlob = (API.__private__.getBlob = function(data) {
return new Blob([getArrayBuffer(data)], {
var getArrayBuffer = (API.__private__.getArrayBuffer = function(content) {
let length = 0;
for (let i = 0; i < content.length; i++) {
let contentLine = content[i];
length += contentLine.length + 1; // +1 for newline
}

let arrayBuffer = new ArrayBuffer(length);
let uint8Array = new Uint8Array(arrayBuffer);
let index = 0;

for (let i = 0; i < content.length; i++) {
let contentLine = content[i];
for (let j = 0; j < contentLine.length; j++) {
uint8Array[index++] = contentLine.charCodeAt(j);
}
uint8Array[index++] = 0x0a; // newline
}

return arrayBuffer;
});

var getBlob = (API.__private__.getBlob = function(content) {
const parts = [];

for (let i = 0; i < content.length; i++) {
let contentLine = content[i];
let arrayBuffer = new ArrayBuffer(contentLine.length + 1); // +1 for newline
let uint8Array = new Uint8Array(arrayBuffer);

for (let j = 0; j < contentLine.length; j++) {
uint8Array[j] = contentLine.charCodeAt(j);
}
uint8Array[contentLine.length] = 0x0a; // newline
parts.push(arrayBuffer);
}

return new Blob(parts, {
type: "application/pdf"
});
});
Expand Down Expand Up @@ -3047,7 +3078,7 @@ function jsPDF(options) {

switch (type) {
case undefined:
return buildDocument();
return getString(buildDocument());
case "save":
API.save(options.filename);
break;
Expand Down Expand Up @@ -3076,7 +3107,7 @@ function jsPDF(options) {
case "datauristring":
case "dataurlstring":
var dataURI = "";
var pdfDocument = buildDocument();
var pdfDocument = getString(buildDocument());
try {
dataURI = btoa(pdfDocument);
} catch (e) {
Expand Down
Binary file modified test/reference/autoPaging10Pages.pdf
Binary file not shown.
Binary file modified test/reference/closed.pdf
Binary file not shown.
Binary file modified test/reference/encrypted_printable.pdf
Binary file not shown.
Binary file modified test/reference/encrypted_standard.pdf
Binary file not shown.
Binary file modified test/reference/encrypted_withAcroForm.pdf
Binary file not shown.
Binary file modified test/reference/encrypted_withImage.pdf
Binary file not shown.
Binary file modified test/reference/html-basic.pdf
Binary file not shown.
Binary file modified test/reference/html-font-faces.pdf
Binary file not shown.
Binary file modified test/reference/html-margin-page-break-slice.pdf
Binary file not shown.
Binary file modified test/reference/html-margin-page-break-text.pdf
Binary file not shown.
Binary file modified test/reference/html-margin-page-break.pdf
Binary file not shown.
Binary file modified test/reference/html-margin-x-y-text.pdf
Binary file not shown.
Binary file modified test/reference/html-width-100-windowWidth-500.pdf
Binary file not shown.
Binary file modified test/reference/html-width-210-windowWidth-1000.pdf
Binary file not shown.
Binary file modified test/reference/html-width-210-windowWidth-250.pdf
Binary file not shown.
Binary file modified test/reference/html-width-210-windowWidth-500.pdf
Binary file not shown.
Binary file modified test/reference/html-width-300-windowWidth-500-scale-2.pdf
Binary file not shown.
Binary file modified test/reference/html-width-300-windowWidth-500.pdf
Binary file not shown.
Binary file modified test/reference/html-width-default-windowWidth-default.pdf
Binary file not shown.
Binary file modified test/reference/multiLineLinkWithText.pdf
Binary file not shown.
Binary file modified test/reference/open.pdf
Binary file not shown.
Binary file modified test/reference/table-autoSize-headerNames.pdf
Binary file not shown.
Binary file modified test/reference/table-autoSize.pdf
Binary file not shown.
Binary file modified test/reference/table-formatted.pdf
Binary file not shown.
Binary file modified test/reference/table.pdf
Binary file not shown.
Binary file modified test/reference/textfieldMultiline.pdf
Binary file not shown.
Binary file modified test/reference/textfieldMultilineSmallForm.pdf
Binary file not shown.
Binary file modified test/reference/webp-multiple.pdf
Binary file not shown.
18 changes: 13 additions & 5 deletions test/specs/jspdf.unit.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -546,20 +546,28 @@ describe("Core: Unit Tests", () => {
});
});

it("jsPDF private function getString", () => {
const doc = jsPDF({ floatPrecision: 2 });
expect(doc.__private__.getString(["A"]).length).toEqual(1);
expect(typeof doc.__private__.getString(["A"]) === "string").toEqual(
true
);
});

it("jsPDF private function getArrayBuffer", () => {
const doc = jsPDF({ floatPrecision: 2 });
expect(doc.__private__.getArrayBuffer("A").byteLength).toEqual(1);
expect(doc.__private__.getArrayBuffer("A") instanceof ArrayBuffer).toEqual(
expect(doc.__private__.getArrayBuffer(["A"]).byteLength).toEqual(2); // +1 for newline
expect(doc.__private__.getArrayBuffer(["A"]) instanceof ArrayBuffer).toEqual(
true
);
});

if (global.isNode !== true) {
it("jsPDF private function getBlob", () => {
const doc = new jsPDF({ floatPrecision: 2 });
expect(typeof doc.__private__.getBlob("A")).toEqual("object");
expect(doc.__private__.getBlob("A") instanceof Blob).toEqual(true);
expect(doc.__private__.getBlob("A").type).toEqual("application/pdf");
expect(typeof doc.__private__.getBlob(["A"])).toEqual("object");
expect(doc.__private__.getBlob(["A"]) instanceof Blob).toEqual(true);
expect(doc.__private__.getBlob(["A"]).type).toEqual("application/pdf");
});
}

Expand Down