how to display base64 encoded pdf?

前端 未结 3 447
被撕碎了的回忆
被撕碎了的回忆 2020-12-01 16:03

I have to display base64 pdf in new tab. I am using below code

 var windo = window.open(\"\", \"\");  
 var objbuilder = \'\';
 objbuilder += (\'

        
3条回答
  •  攒了一身酷
    2020-12-01 16:33

    function printPreview(data){
            var type = 'application/pdf';
            let blob = null;
            const blobURL = URL.createObjectURL( pdfBlobConversion(data, 'application/pdf'));
            const theWindow = window.open(blobURL);
            const theDoc = theWindow.document;
            const theScript = document.createElement('script');
            function injectThis() {
                window.print();
            }
            theScript.innerHTML = 'window.onload = ${injectThis.toString()};';
            theDoc.body.appendChild(theScript);
        }
      //converts base64 to blob type for windows
        function pdfBlobConversion(b64Data, contentType) {
              contentType = contentType || '';
              var sliceSize = 512;
              b64Data = b64Data.replace(/^[^,]+,/, '');
              b64Data = b64Data.replace(/\s/g, '');
              var byteCharacters = window.atob(b64Data);
              var byteArrays = [];
    
              for ( var offset = 0; offset < byteCharacters.length; offset = offset + sliceSize ) {
                var slice = byteCharacters.slice(offset, offset + sliceSize);
    
                var byteNumbers = new Array(slice.length);
                for (var i = 0; i < slice.length; i++) {
                  byteNumbers[i] = slice.charCodeAt(i);
                }
    
                var byteArray = new Uint8Array(byteNumbers);
    
                byteArrays.push(byteArray);
              }
    
              var blob = new Blob(byteArrays, { type: contentType });
              return blob;
            }
    

    pass base64 data to this function and it will show the pdf in new window. This is working in chrome and firefox not in IE. Any help Please. I want it to work in IE also. Its giving error where I give the bloburl to window.

提交回复
热议问题