Skip to content

Commit 41bcbfa

Browse files
UzlopakMrRio
authored andcommitted
Update addimage.js (#1115)
Solution to solve memory leaks caused by big images. Is solving the issues #844 and #425
1 parent d8118f5 commit 41bcbfa

File tree

1 file changed

+13
-32
lines changed

1 file changed

+13
-32
lines changed

plugins/addimage.js

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -397,40 +397,21 @@
397397
};
398398

399399
/**
400-
* @see this discussion
401-
* http://stackoverflow.com/questions/6965107/converting-between-strings-and-arraybuffers
402-
*
403-
* As stated, i imagine the method below is highly inefficent for large files.
404-
*
405-
* Also of note from Mozilla,
406-
*
407-
* "However, this is slow and error-prone, due to the need for multiple conversions (especially if the binary data is not actually byte-format data, but, for example, 32-bit integers or floats)."
408-
*
409-
* https://developer.mozilla.org/en-US/Add-ons/Code_snippets/StringView
410-
*
411-
* Although i'm strugglig to see how StringView solves this issue? Doesn't appear to be a direct method for conversion?
412-
*
413-
* Async method using Blob and FileReader could be best, but i'm not sure how to fit it into the flow?
400+
* Convert the Buffer to a Binary String
414401
*/
415402
jsPDFAPI.arrayBufferToBinaryString = function(buffer) {
416-
/*if('TextDecoder' in window){
417-
var decoder = new TextDecoder('ascii');
418-
return decoder.decode(buffer);
419-
}*/
420-
421-
if(this.isArrayBuffer(buffer))
422-
buffer = new Uint8Array(buffer);
423-
424-
var binary_string = '';
425-
var len = buffer.byteLength;
426-
for (var i = 0; i < len; i++) {
427-
binary_string += String.fromCharCode(buffer[i]);
428-
}
429-
return binary_string;
430-
/*
431-
* Another solution is the method below - convert array buffer straight to base64 and then use atob
432-
*/
433-
//return atob(this.arrayBufferToBase64(buffer));
403+
if (typeof(window.atob) === "function") {
404+
return atob(this.arrayBufferToBase64(buffer));
405+
} else {
406+
var data = (this.isArrayBuffer(buffer)) ? buffer : new Uint8Array(buffer);
407+
var chunkSizeForSlice = 0x5000;
408+
var binary_string = '';
409+
var slicesCount = Math.round(data.byteLength / chunkSizeForSlice);
410+
for (var i = 0; i < slicesCount; i++) {
411+
binary_string += String.fromCharCode.apply(null, data.slice(i*chunkSizeForSlice, i*chunkSizeForSlice+chunkSizeForSlice));
412+
}
413+
return binary_string;
414+
}
434415
};
435416

436417
/**

0 commit comments

Comments
 (0)