I found Rotativa as an easy way to export to PDF (works almost perfectly except CSS3 doesn't seem to be supported, maybe in a future version) ... but I wonder how to handle the FileName
option.
When I
return new Rotativa.ViewAsPdf("myViewName", "~/Views/Shared/_PDFLayout.cshtml", myModel)
{
FileName = "myCorrectlyNamed.pdf",
PageSize = ... // plus some more options
};
then I'll get myCorrectlyNamed.pdf
for download. When I omit the FileName
option, the PDF is displayed in the browser, but when I save it from there, it has just the default filename document.pdf
.
How to generate and display the pdf in the browser and have a filename other than document.pdf
when it's saved from there?
You can add Content-Disposition
header to the Response
. Using this header, you'll be able to tell the browser to display the file inline and also specify the name that should be used when user tries to Save the file.
Response.AppendHeader("Content-Disposition", new System.Net.Mime.ContentDisposition { Inline = true, FileName = "myCorrectlyNamed.pdf" }.ToString());
return new Rotativa.ViewAsPdf("myViewName", "~/Views/Shared/_PDFLayout.cshtml", myModel)
{
PageSize = ... // plus some more options
};
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