|
397 | 397 | };
|
398 | 398 |
|
399 | 399 | /**
|
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 |
414 | 401 | */
|
415 | 402 | 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 | + } |
434 | 415 | };
|
435 | 416 |
|
436 | 417 | /**
|
|
0 commit comments