@@ -45,12 +45,12 @@ class Pdf extends React.Component {
4545 }
4646
4747 onGetPdfRaw ( pdfRaw ) {
48- const { onContentAvailable, onBinaryContentAvailable, binaryToBase64} = this . props ;
48+ const { onContentAvailable, onBinaryContentAvailable, binaryToBase64 } = this . props ;
4949 if ( typeof onBinaryContentAvailable === 'function' ) {
5050 onBinaryContentAvailable ( pdfRaw ) ;
5151 }
5252 if ( typeof onContentAvailable === 'function' ) {
53- var convertBinaryToBase64 = this . defaultBinaryToBase64 ;
53+ let convertBinaryToBase64 = this . defaultBinaryToBase64 ;
5454 if ( typeof binaryToBase64 === 'function' ) {
5555 convertBinaryToBase64 = binaryToBase64 ;
5656 }
@@ -109,43 +109,27 @@ class Pdf extends React.Component {
109109 }
110110 }
111111
112- renderPdf ( ) {
113- const { page } = this . state ;
114- if ( page ) {
115- const { canvas } = this . refs ;
116- const canvasContext = canvas . getContext ( '2d' ) ;
117- const { scale } = this . props ;
118- const viewport = page . getViewport ( scale ) ;
119- canvas . height = viewport . height ;
120- canvas . width = viewport . width ;
121- page . render ( { canvasContext, viewport } ) ;
122- }
123- }
124-
125- render ( ) {
126- const { loading } = this . props ;
127- const { page } = this . state ;
128- return page ? < canvas ref = "canvas" /> : loading || < div > Loading PDF...</ div > ;
129- }
130-
131112 // Converts an ArrayBuffer directly to base64, without any intermediate 'convert to string then
132113 // use window.btoa' step and without risking a blow of the stack. According to [Jon Leightons's]
133114 // tests, this appears to be a faster approach: http://jsperf.com/encoding-xhr-image-data/5
134115 // Jon Leighton https://gist.github.com/jonleighton/958841
135116 defaultBinaryToBase64 ( arrayBuffer ) {
136- var base64 = '' ;
137- var encodings = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' ;
117+ let base64 = '' ;
118+ const encodings = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' ;
138119
139- var bytes = new Uint8Array ( arrayBuffer ) ;
140- var byteLength = bytes . byteLength ;
141- var byteRemainder = byteLength % 3 ;
142- var mainLength = byteLength - byteRemainder ;
120+ const bytes = new Uint8Array ( arrayBuffer ) ;
121+ const byteLength = bytes . byteLength ;
122+ const byteRemainder = byteLength % 3 ;
123+ const mainLength = byteLength - byteRemainder ;
143124
144- var a , b , c , d ;
145- var chunk ;
125+ let a ;
126+ let b ;
127+ let c ;
128+ let d ;
129+ let chunk ;
146130
147131 // Main loop deals with bytes in chunks of 3
148- for ( var i = 0 ; i < mainLength ; i = i + 3 ) {
132+ for ( let i = 0 ; i < mainLength ; i = i + 3 ) {
149133 // Combine the three bytes into a single integer
150134 chunk = ( bytes [ i ] << 16 ) | ( bytes [ i + 1 ] << 8 ) | bytes [ i + 2 ] ;
151135
@@ -156,7 +140,7 @@ class Pdf extends React.Component {
156140 d = chunk & 63 ; // 63 = 2^6 - 1
157141
158142 // Convert the raw binary segments to the appropriate ASCII encoding
159- base64 += encodings [ a ] + encodings [ b ] + encodings [ c ] + encodings [ d ] ;
143+ base64 = [ base64 , encodings [ a ] , encodings [ b ] , encodings [ c ] , encodings [ d ] ] . join ( '' ) ;
160144 }
161145
162146 // Deal with the remaining bytes and padding
@@ -168,7 +152,7 @@ class Pdf extends React.Component {
168152 // Set the 4 least significant bits to zero
169153 b = ( chunk & 3 ) << 4 ; // 3 = 2^2 - 1
170154
171- base64 += encodings [ a ] + encodings [ b ] + '=='
155+ base64 = [ base64 , encodings [ a ] , encodings [ b ] , '==' ] . join ( '' ) ;
172156 } else if ( byteRemainder === 2 ) {
173157 chunk = ( bytes [ mainLength ] << 8 ) | bytes [ mainLength + 1 ] ;
174158
@@ -178,13 +162,32 @@ class Pdf extends React.Component {
178162 // Set the 2 least significant bits to zero
179163 c = ( chunk & 15 ) << 2 ; // 15 = 2^4 - 1
180164
181- base64 += encodings [ a ] + encodings [ b ] + encodings [ c ] + '=' ;
165+ base64 = [ base64 , encodings [ a ] , encodings [ b ] , encodings [ c ] , '=' ] . join ( '' ) ;
182166 }
183167
184- return base64
168+ return base64 ;
185169 }
186170
171+ renderPdf ( ) {
172+ const { page } = this . state ;
173+ if ( page ) {
174+ const { canvas } = this . refs ;
175+ const canvasContext = canvas . getContext ( '2d' ) ;
176+ const { scale } = this . props ;
177+ const viewport = page . getViewport ( scale ) ;
178+ canvas . height = viewport . height ;
179+ canvas . width = viewport . width ;
180+ page . render ( { canvasContext, viewport } ) ;
181+ }
182+ }
183+
184+ render ( ) {
185+ const { loading } = this . props ;
186+ const { page } = this . state ;
187+ return page ? < canvas ref = "canvas" /> : loading || < div > Loading PDF...</ div > ;
188+ }
187189}
190+
188191Pdf . displayName = 'react-pdf-js' ;
189192Pdf . propTypes = {
190193 content : React . PropTypes . string ,
0 commit comments