The Objective:
I have to print a PDF on a new tab after some tasks have finished correctly.
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:
Controller: Export
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.
You can set the content-type as application/pdf in the response header just like Content-Type: application/pdf
And in order to open it to new tab in javascript, please add this code.
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
.
And add download HTML5 attribute to a tag or just 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.
$("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)
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With