最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to show PDF in a new tab of IE without downloading it - Stack Overflow

programmeradmin7浏览0评论

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;
    }
}

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;
    }
}
Share Improve this question asked Mar 13, 2018 at 23:33 John GlabbJohn Glabb 1,6218 gold badges30 silver badges64 bronze badges
Add a comment  | 

6 Answers 6

Reset to default 7 +50

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.

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.

try pdfjs https://mozilla.github.io/pdf.js/

Set up Viewer.php where you pass the link of the pdf file to show, and simply open it in a new tab, e.g example.com/viewer.php?pdfLink=/files/path/to/doc.pdf

<script>
function pdf() {
window.open("filename.pdf","_blank");
}
</script>
<div onclick="pdf();">Click here</div>

Is this what you want? This doesn't uses anchor tag and is supported in all browsers. Click here for more information.

You may try this angular package(https://www.npmjs.com/package/ng2-pdfjs-viewer). It has an easy option to open pdf in a new tab. Use the attribute [externalWindow]="true".

Code
<ng2-pdfjs-viewer [externalWindow]="true" pdfSrc="samplepdf.pdf"></ng2-pdfjs-viewer>

I have done it in all browsers using backend code. All you need to do is open a new web page in new tab from front end. let's say - www.example.com/path/to/pdf_file.

In the backend code for this get request, you need to read the pdf file and put the content in response body.

Set the Content-disposition header to inline;filename=<your pdf filename> This will ensure the response content should not be downloaded.

I would have shared the backend code to it, if I knew what backend you are using.

发布评论

评论列表(0)

  1. 暂无评论