I am trying to generate a PDF file based on the response returned from the server and open the file in new tab. It works very well in Desktop across all browsers but has problem in apple devices (iphone + ipad) in safari browser.
Here is the code snippet:
if (responseType = base64 encoded string like JVBERi0xLjUKJeLjz9MKMSAwI....) {
const binaryString = window.atob(fileResponseData);
const bytes = new Uint8Array(binaryString.length);
const binaryToBlob = bytes.map((byte, i) => binaryString.charCodeAt(i));
const blob = new Blob([binaryToBlob], { type: 'application/pdf' });
this.downloadFile(blob, fileName);
} else {
// blob response like %PDF-1.7 %âãÏÓ5 0 obj....
const blob = new Blob([fileResponseData], { type: 'application/pdf' });
this.downloadFile(blob, fileName);
}
This is how I am downloading the file
if (window.navigator && window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveOrOpenBlob(blob, fileName);
return;
}
const url = (window.URL || window.webkitURL).createObjectURL(blob);
window.open(url, '_blank');
I know there are related articles regarding this topic, but they didn't solved my problem. Infact I came up with above code referring to those articles itself but still I am facing problem in apple devices. As soon as I click button to generate file and display in new tab, nothing happens on apple device, but other devices works just fine.
I had same problem in my react application where I wanted to open pdf blob url in new tab, iPad safari didn't allow me to do so but other devices and browses allowed. Please refer below code when you wanted to open the pdf blob url in new tab on ipad safari: