Hi, I am working in an Angular web app, in which I am getting a base64 string from a backend API, which represents a PDF file. I want to open that PDF in a new tab. I have tried using Blobs, iframes, and all sorts of methods, all of which seem to work in my Mac's chrome and safari, and they even work on a simulated iPad (using XCode and the simulated device's safari) but they don't work on the real device (iPad). This is the code I use to open a PDF in a new tab, both supporting chrome and safari:
First, I determine if the browser is safari. If this is the case, I pre-open the new window, as opening a new tab from inside an XHR call is forbidden in safari:
Then, I make my XHR call to get the PDF url in base64, and I create a Blob out of it:
I have tried this and many other approaches, and none of them work on a real iPad, even though they do work on a simulated iPad.
Can you provide me with any guidance about how to get the PDF in base64 to properly display in a new tab in an iPad? I need it to work in iOs Safari or in Chrome for iOs (I understand they are both internally very similar).
Thank you in advance.
First, I determine if the browser is safari. If this is the case, I pre-open the new window, as opening a new tab from inside an XHR call is forbidden in safari:
Code Block TypeScript var isSafari = navigator.vendor && navigator.vendor.indexOf('Apple') > -1 && navigator.userAgent && navigator.userAgent.indexOf('CriOS') == -1 && navigator.userAgent.indexOf('FxiOS') == -1; if (isSafari) { var newWindow = window.open('', '_blank'); }
Then, I make my XHR call to get the PDF url in base64, and I create a Blob out of it:
Code Block TypeScript this.dataService .getPDFUrl(this.data.code) .subscribe((res) => { this.pdfUrl = res.message; var byteCharacters = atob(this.pdfUrl); var byteNumbers = new Array(byteCharacters.length); for (var i = 0; i < byteCharacters.length; i++) { byteNumbers[i] = byteCharacters.charCodeAt(i); } var byteArray = new Uint8Array(byteNumbers); var file = new Blob([byteArray], { type: 'application/pdf;base64' }); this.pdfUrl = (window.URL || window.webkitURL).createObjectURL(file); //If browser is Safari, use a Reader to display the PDF in the previously opened window if (isSafari) { var reader = new FileReader(); reader.onloadend = function (e) { newWindow.location.href = reader.result as string; }; reader.readAsDataURL(file); } else { //If browser is not Safari, open a new window (works in chrome) window.open(this.pdfUrl, '_blank'); } });
I have tried this and many other approaches, and none of them work on a real iPad, even though they do work on a simulated iPad.
Can you provide me with any guidance about how to get the PDF in base64 to properly display in a new tab in an iPad? I need it to work in iOs Safari or in Chrome for iOs (I understand they are both internally very similar).
Thank you in advance.