decode and download pdf stream
Tue Dec 07 2021 23:41:36 GMT+0000 (UTC)
Saved by @davidpetrey #javascript #pdf
$('#downloadRegistrationCertificate').on('click',function(){ downloadPdf(); }); function downloadPdf() { var regNum = JSON.parse(sessionStorage.getItem("sessionPath")).regNumber $.ajax({ url: apiPathEnv + "/web/registration-service/v1/download-certificate?registrationNumber="+regNum+"&sessionToken="+sessionTokenVar, type: 'get', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' } }) .done(function (result) { if(result != undefined && (result.messages == null || result.messages.length == 0)){ console.log("REsults",result.bytes); var responseBytes = _base64ToArrayBuffer(result.bytes); var certificateBlob = new Blob([responseBytes], {type: 'application/pdf'}); // console.log(responseBytes); var downloadLink = document.createElement('a'); downloadLink.href = window.URL.createObjectURL(certificateBlob); //downloadLink.download="RegistrationCertificate_" +regNum+ ".pdf"; //downloadLink.click(); var isIE = /*@cc_on!@*/false || !!document.documentMode; var isSafari = /constructor/i.test(window.HTMLElement) || (function (p) { return p.toString() === "[object SafariRemoteNotification]"; })(!window['safari'] || (typeof safari !== 'undefined' && window['safari'].pushNotification)); if(isIE) { // Internet Explorer 6-11 window.navigator.msSaveBlob(certificateBlob, 'RegistrationCertificate.pdf'); } else if(isSafari) { // Safari Browser var newWindow = window.open(window.URL.createObjectURL(certificateBlob),'_blank') } else { // Mobile Devices if(/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)){ downloadLink.download="RegistrationCertificate_" +regNum+ ".pdf"; downloadLink.click(); } // Other Browsers else { window.open(window.URL.createObjectURL(certificateBlob), '_blank'); } } } }).fail(function (result){ console.log(result); // api .fail error page redirect siteNotAvailable(); }); } function _base64ToArrayBuffer(base64) { var binary_string = window.atob(base64); var len = binary_string.length; var bytes = new Uint8Array(len); for (var i = 0; i < len; i++) { bytes[i] = binary_string.charCodeAt(i); } return bytes.buffer; }
Comments