[英]How to Open .pdf on a new Tab
The Objective:目标:
I have to print a PDF on a new tab after some tasks have finished correctly.一些任务正确完成后,我必须在新选项卡上打印 PDF。
Steps: I want to execute a method that should go to the server, take the PDF and open it on a new Tab, I was trying with these but is not working:步骤:我想对服务器执行一个应该为 go 的方法,获取 PDF 并在新选项卡上打开它,我正在尝试使用这些但无法正常工作:
Controller : Export Controller : 出口
public ActionResult PrintPdf()
{
Response.AppendHeader("Content-Disposition", "inline; filename= " + MyClassPdfWriter.GetFileName);
return File(MyClassPdfWriter.GetMemoryStream, "application/pdf");
}
View :查看:
function TasksSucced(){
$.get('@Url.Action("PrintPdf", "Export", "new {target = _blank}")');
}
Solved!解决了! That works for me
这对我行得通
window.open('/Export/PrintPdf');
There several ways to download or view the PDF.有几种方法可以下载或查看 PDF。
You can set the content-type as application/pdf in the response header just like Content-Type: application/pdf
您可以在响应标头中将内容类型设置为application/pdf ,就像
Content-Type: application/pdf
And in order to open it to new tab in javascript, please add this code.为了在 javascript 中将其打开到新选项卡,请添加此代码。
window.open("Here Download PDF url", '_blank');
You need to set the content-type as application/octet-stream in the response header just like application/octet-stream
.您需要在响应标头中将内容类型设置为application/octet-stream ,就像
application/octet-stream
一样。
And add download HTML5 attribute to a tag or just target="_blank" .并将下载HTML5 属性添加到标签或只是target="_blank" 。
<a href="PDF URL" download="pdf">Download</a>
<a href="PDF URL" target="_blank">Download</a>
So you can use PDF.js or 3rd library to view the PDF in iFrame or inline page.因此,您可以使用 PDF.js 或 3rd 库在 iFrame 或内联页面中查看 PDF。
$("a[target!='_blank'][href$='.pdf']").attr("target", "_blank");
If anyone is having a similar problem, none of the above solutions worked for me in Google Chrome (they did work in Firefox)如果有人遇到类似的问题,上述解决方案在 Google Chrome 中都不适合我(它们在 Firefox 中确实有效)
This code worked in all major browsers:此代码适用于所有主要浏览器:
var a = document.createElement('A');
var filePath = 'https://pathtopdf.com/file.pdf';
a.href = filePath;
a.download = filePath.substr(filePath.lastIndexOf('/') + 1);
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
You can use the following solution您可以使用以下解决方案
jQuery('<form target="_blank" action="' + URL + '" method="get"></form>').appendTo('body').submit().remove();
where URL is the pdf url...其中 URL 是 pdf 网址...
You can try using jQuery.deffered .您可以尝试使用jQuery.defered 。 I prepared sample function for you.
我为你准备了示例函数。 It will process getPdf call, and then will resolve promise.
它将处理 getPdf 调用,然后将解决 promise。 Hope this helps
希望这可以帮助
function getPDF() {
return '';
}
getPdf()
.then(function(response) {
console.log('response here' + response);
window.open(response);
}).fail(function(){
console.error('failed reposne');
});
This will convert the base64 to pdf and then open in new Tab.这会将 base64 转换为 pdf,然后在新选项卡中打开。
export default function base64toPdfInNewTab(pdf: string) {
var binary = atob(pdf.replace(/\s/g, ""));
var len = binary.length;
var buffer = new ArrayBuffer(len);
var view = new Uint8Array(buffer);
for (var i = 0; i < len; i++) {
view[i] = binary.charCodeAt(i);
}
const file = new Blob([view], {
type: "application/pdf",
});
const fileURL = URL.createObjectURL(file);
window.open(fileURL, "_blank");
}
Parameter pdf is the base64 string.参数 pdf 是base64字符串。
https://stackoverflow.com/a/72527783 https://stackoverflow.com/a/72527783
It's work me.这是我的工作。
My sample code我的示例代码
var order_id = $(this).data('order_id');
var url = 'pdf url';
var data = {
order_id
};
var myHeaders = new Headers();
myHeaders.append("Content-Type", "application/x-www-form-urlencoded");
var urlencoded = new URLSearchParams();
urlencoded.append("param", JSON.stringify(data));
var requestOptions = {
method : 'POST',
headers : myHeaders,
body : urlencoded,
redirect: 'follow'
};
fetch(url, requestOptions)
.then((response) => response.blob())
.then((blob) => {
const _url = window.URL.createObjectURL(blob);
window.open(_url, '_blank');
}).catch((err) => {
console.log(err);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.