[英]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.