From d2f0b4e4250d6f47b5757329328719d84fa51b56 Mon Sep 17 00:00:00 2001 From: shiina-mash1ro <50709672+shiina-mash1ro@users.noreply.github.com> Date: Tue, 8 Aug 2023 16:36:24 +0800 Subject: [PATCH 1/3] Fix Invalid String Length When Saving PDF #1724 --- src/jspdf.js | 52 ++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 44 insertions(+), 8 deletions(-) diff --git a/src/jspdf.js b/src/jspdf.js index dc009f313..55706f484 100644 --- a/src/jspdf.js +++ b/src/jspdf.js @@ -785,6 +785,10 @@ function jsPDF(options) { }); var getArrayBuffer = (API.__private__.getArrayBuffer = function(data) { + if (data instanceof ArrayBuffer) { + return data; + } + var len = data.length, ab = new ArrayBuffer(len), u8 = new Uint8Array(ab); @@ -2975,7 +2979,13 @@ function jsPDF(options) { } }); - var buildDocument = (API.__private__.buildDocument = function() { + /** + * @param {'string'|'arrayBuffer'} [returnType='string'] - the desired return type + * @returns {string|ArrayBuffer} + */ + var buildDocument = (API.__private__.buildDocument = function( + returnType = "string" + ) { resetDocument(); setOutputDestination(content); @@ -2998,7 +3008,31 @@ function jsPDF(options) { setOutputDestination(pages[currentPage]); - return content.join("\n"); + if (returnType === "arrayBuffer") { + return getContentArrayBuffer(); + } else { + return content.join("\n"); + } + }); + + var getContentArrayBuffer = (API.__private__.getContentArrayBuffer = function() { + let length = 0; + for (let contentItem of content) { + length += contentItem.length; + ++length; // newline + } + + let arrayBuffer = new ArrayBuffer(length); + let uint8Array = new Uint8Array(arrayBuffer); + + for (let contentItem of content) { + for (let i = 0; i < contentItem.length; i++) { + uint8Array[i] = contentItem.charCodeAt(i); + } + uint8Array[contentItem.length] = 0x0a; // newline + } + + return arrayBuffer; }); var getBlob = (API.__private__.getBlob = function(data) { @@ -3052,9 +3086,9 @@ function jsPDF(options) { API.save(options.filename); break; case "arraybuffer": - return getArrayBuffer(buildDocument()); + return getArrayBuffer(buildDocument("arrayBuffer")); case "blob": - return getBlob(buildDocument()); + return getBlob(buildDocument("arrayBuffer")); case "bloburi": case "bloburl": // Developer is responsible of calling revokeObjectURL @@ -3064,7 +3098,9 @@ function jsPDF(options) { ) { return ( (globalObject.URL && - globalObject.URL.createObjectURL(getBlob(buildDocument()))) || + globalObject.URL.createObjectURL( + getBlob(buildDocument("arrayBuffer")) + )) || void 0 ); } else { @@ -5872,7 +5908,7 @@ function jsPDF(options) { // @if MODULE_FORMAT!='cjs' if (options.returnPromise === false) { - saveAs(getBlob(buildDocument()), filename); + saveAs(getBlob(buildDocument("arrayBuffer")), filename); if (typeof saveAs.unload === "function") { if (globalObject.setTimeout) { setTimeout(saveAs.unload, 911); @@ -5882,7 +5918,7 @@ function jsPDF(options) { } else { return new Promise(function(resolve, reject) { try { - var result = saveAs(getBlob(buildDocument()), filename); + var result = saveAs(getBlob(buildDocument("arrayBuffer")), filename); if (typeof saveAs.unload === "function") { if (globalObject.setTimeout) { setTimeout(saveAs.unload, 911); @@ -5899,7 +5935,7 @@ function jsPDF(options) { // @if MODULE_FORMAT='cjs' // eslint-disable-next-line no-unreachable var fs = require("fs"); - var buffer = Buffer.from(getArrayBuffer(buildDocument())); + var buffer = Buffer.from(getArrayBuffer(buildDocument("arrayBuffer"))); if (options.returnPromise === false) { fs.writeFileSync(filename, buffer); } else { From b52907a4ef9abcbae2608d9fae399eda90e70541 Mon Sep 17 00:00:00 2001 From: shiina-mashiro <50709672+shiina-mash1ro@users.noreply.github.com> Date: Tue, 8 Aug 2023 16:49:59 +0800 Subject: [PATCH 2/3] Update continuous-integration.yml make it can run by manual --- .github/workflows/continuous-integration.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/continuous-integration.yml b/.github/workflows/continuous-integration.yml index e69a92dd4..5800004db 100644 --- a/.github/workflows/continuous-integration.yml +++ b/.github/workflows/continuous-integration.yml @@ -1,6 +1,6 @@ name: Continous Integration -on: [push, pull_request] +on: [push, pull_request, workflow_dispatch] jobs: test-saucelabs: From b1e18620ce5af8246bf096cc1f7fc17ccf51d5de Mon Sep 17 00:00:00 2001 From: shiina-mash1ro <50709672+shiina-mash1ro@users.noreply.github.com> Date: Tue, 8 Aug 2023 17:57:42 +0800 Subject: [PATCH 3/3] fix wrong index --- src/jspdf.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/jspdf.js b/src/jspdf.js index 55706f484..047a3d24a 100644 --- a/src/jspdf.js +++ b/src/jspdf.js @@ -3024,12 +3024,13 @@ function jsPDF(options) { let arrayBuffer = new ArrayBuffer(length); let uint8Array = new Uint8Array(arrayBuffer); + let index = 0; for (let contentItem of content) { for (let i = 0; i < contentItem.length; i++) { - uint8Array[i] = contentItem.charCodeAt(i); + uint8Array[index++] = contentItem.charCodeAt(i); } - uint8Array[contentItem.length] = 0x0a; // newline + uint8Array[index++] = 0x0a; // newline } return arrayBuffer;