Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show PDF in a new tab of IE without downloading it

Seems IE won't allow to open blobs directly. You have to use msSaveOrOpenBlob. But is there any way to convert it somehowe then. I do need to show PDF into a new tab of IE without downloading it or at least user should not interact and don't see it's being downloaded into e.g. system temp folder. Safai opens it in same window instead of a new tab but main problem is IE. The main idea to make all browsers work similar and open it in new tab for preview.

let blob = new Blob([response], {
    type: 'application/pdf'
});

if (window.navigator && window.navigator.msSaveOrOpenBlob) {//IE
    window.navigator.msSaveOrOpenBlob(response, "TheDocumentToShow.pdf");
} else {
    var fileURL = URL.createObjectURL(response);
    if (navigator.userAgent.indexOf("Chrome") != -1 || navigator.userAgent.indexOf("Firefox") != -1){
        let win = window.open(fileURL, '_blank');                        
    } else { //Safari & Opera iOS
        window.location.href = fileURL;
    }
}
like image 802
John Glabb Avatar asked Mar 13 '18 23:03

John Glabb


2 Answers

you could use this library: https://github.com/VadimDez/ng2-pdf-viewer. It is build on pdfjs and can integrate a pdf into your own page. You can create a route which takes a link as paramater and open that in a new tab.

like image 53
Robin Dijkhof Avatar answered Nov 12 '22 07:11

Robin Dijkhof


This simple a link works on IE11, Edge and Chrome:

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <a href="file.pdf" target="_blank">Click</a>
</body>
</html>

It opens the pdf in a new tab without downloading it.

like image 35
Dhruv Murarka Avatar answered Nov 12 '22 07:11

Dhruv Murarka