@@ -43,6 +43,10 @@ import { atob, btoa } from "../libs/AtobBtoa.js";
43
43
44
44
var UNKNOWN = "UNKNOWN" ;
45
45
46
+ // Heuristic limit after which String.fromCharCode will start to overflow.
47
+ // Need to change to StringDecoder.
48
+ var ARRAY_BUFFER_LIMIT = 6e6 ;
49
+
46
50
var imageFileTypeHeaders = {
47
51
PNG : [ [ 0x89 , 0x50 , 0x4e , 0x47 ] ] ,
48
52
TIFF : [
@@ -730,20 +734,28 @@ import { atob, btoa } from "../libs/AtobBtoa.js";
730
734
var arrayBufferToBinaryString = ( jsPDFAPI . __addimage__ . arrayBufferToBinaryString = function (
731
735
buffer
732
736
) {
733
- try {
734
- return atob ( btoa ( String . fromCharCode . apply ( null , buffer ) ) ) ;
735
- } catch ( e ) {
736
- if (
737
- typeof Uint8Array !== "undefined" &&
738
- typeof Uint8Array . prototype . reduce !== "undefined"
739
- ) {
740
- return new Uint8Array ( buffer )
741
- . reduce ( function ( data , byte ) {
742
- return data . push ( String . fromCharCode ( byte ) ) , data ;
743
- } , [ ] )
744
- . join ( "" ) ;
737
+ // Skip direct conversion as it might take many hundred milliseconds before throwing.
738
+ if ( buffer . length < ARRAY_BUFFER_LIMIT ) {
739
+ try {
740
+ return atob ( btoa ( String . fromCharCode . apply ( null , buffer ) ) ) ;
741
+ } catch ( e ) {
742
+ // Buffer was overflown, fall back to other methods
745
743
}
746
744
}
745
+ // Text decoder is much faster than string concatenation or reduce/join.
746
+ if ( typeof TextDecoder !== "undefined" ) {
747
+ var decoder = new TextDecoder ( "ascii" ) ;
748
+ return decoder . decode ( buffer ) ;
749
+ } else if (
750
+ typeof Uint8Array !== "undefined" &&
751
+ typeof Uint8Array . prototype . reduce !== "undefined"
752
+ ) {
753
+ return new Uint8Array ( buffer )
754
+ . reduce ( function ( data , byte ) {
755
+ return data . push ( String . fromCharCode ( byte ) ) , data ;
756
+ } , [ ] )
757
+ . join ( "" ) ;
758
+ }
747
759
} ) ;
748
760
749
761
/**
0 commit comments