简体   繁体   English

如何在IE的新选项卡中显示PDF而不下载它

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

Seems IE won't allow to open blobs directly. 似乎IE不允许直接打开blob。 You have to use msSaveOrOpenBlob. 你必须使用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 eg system temp folder. 我确实需要在没有下载的情况下将PDF显示到IE的新选项卡中,或者至少用户不应该进行交互,并且不会将其下载到例如系统临时文件夹中。 Safai opens it in same window instead of a new tab but main problem is IE. Safai在同一个窗口打开它而不是新标签,但主要问题是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;
    }
}

you could use this library: https://github.com/VadimDez/ng2-pdf-viewer . 你可以使用这个库: https//github.com/VadimDez/ng2-pdf-viewer It is build on pdfjs and can integrate a pdf into your own page. 它建立在pdfjs之上 ,可以将pdf集成到您自己的页面中。 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: 这个简单a链接适用于IE11,Edge和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. 它在新标签页中打开pdf而不下载它。

try pdfjs https://mozilla.github.io/pdf.js/ 试试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, eg example.com/viewer.php?pdfLink=/files/path/to/doc.pdf 设置Viewer.php ,您Viewer.php在其中传递要显示的pdf文件的链接,只需在新选项卡中打开它,例如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 ). 您可以尝试这个角度包( https://www.npmjs.com/package/ng2-pdfjs-viewer )。 It has an easy option to open pdf in a new tab . 它有一个简单的选项,可以在新选项卡中打开pdf Use the attribute [externalWindow]="true" . 使用属性[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. 让我们说 - 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. 在此get请求的后端代码中,您需要阅读pdf文件并将内容放在响应正文中。

Set the Content-disposition header to inline;filename=<your pdf filename> This will ensure the response content should not be downloaded. Content-disposition标头设置为inline;filename=<your pdf filename>这将确保不应下载响应内容。

I would have shared the backend code to it, if I knew what backend you are using. 如果我知道你正在使用什么后端,我会分享它的后端代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在不打开新标签或更改当前标签视图的情况下下载 pdf - Downloading a pdf without opening new tab or changing current tab view 在IE11的新选项卡上打开PDF而不提示-mssaveoropenblob - Open PDF in IE11 on new tab without prompting - mssaveoropenblob 如何使IE在新标签页中打开iframe pdf链接? - How to make IE open iframe pdf links in a new tab? 如何在新选项卡中开始下载 pdf 并在 angular 中下载结束时关闭该选项卡 - how to start downloading pdf in new tab and close that tab when download is over in angular 如何在不下载的情况下显示 PDF - How to display a PDF without downloading 单击链接而不下载时如何在新选项卡中显示pdf? - how to display pdf in new tab when clicking on link without download? 如何在没有弹出窗口阻止程序的情况下将PDF下载到新的选项卡/窗口? - How to download a PDF to a new tab/window without popup blocker? 如何让浏览器在新标签页中打开pdf文件,而不仅仅是下载它? - How do I get the browser to open the pdf in a new tab instead of just downloading it? 如何在新标签页上打开.pdf - How to Open .pdf on a new Tab 如何打开这个文件? 或改为下载,在新窗口中显示预览?? 我有pdf档案 - How to open this file?? or Instead Downloading, show preview in new window?? I have pdf file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM