4

I need to set .pdf name before displaying it. I tried to set it like this

return new ViewAsPdf(GetViewName(), "", reportVM) 
{ 
   PageSize = Size.Letter, FileName = GetViewName() + "-" + DateTime.Now.ToShortDateString() + ".pdf"
 }; ,

but in this way the .pdf will be automatically downloaded. Is it possible to set the .pdf name and not downloading it?

Thanks!

Ioana Stoian
  • 146
  • 1
  • 10

4 Answers4

4
var pdfResult = new Rotativa.PartialViewAsPdf("YourPartialView", model) {
    SaveOnServerPath = path, // Save your place
    PageWidth = 200,
    PageHeight = 350,
};

// This section allows you to save without downloading 

pdfResult.BuildPdf(this.ControllerContext);
return RedirectToAction("Index", "Home");
fero
  • 6,050
  • 1
  • 33
  • 56
Bora Karaca
  • 436
  • 5
  • 14
3

After long research, the answer is no. The only thing that I could do is to set file name using response header and remove property FileName, so the code is now:

Response.AppendHeader("Content-Disposition", "inline; filename=" +GetViewName() + "_" + DateTime.Now.ToShortDateString() + ".pdf");
return new ViewAsPdf(GetViewName(), "", reportVM) { PageSize = Size.Letter };

Maybe there is a better solution, but that's the best I could get after all the reading. Hope it will help others too!

Ioana Stoian
  • 146
  • 1
  • 10
  • If you want to display a pdf, you could do it via an ` – Bon Macalindong Aug 16 '16 at 09:54
1
Rotativa.AspNetCore.Options.Margins margins = new Rotativa.AspNetCore.Options.Margins(10, 2, 5, 2); 
        return new ViewAsPdf(estimate)
        {
            CustomSwitches = "--viewport-size 1024x768",
            PageMargins = margins,
            FileName = "Estimate -" + estimate.EstimateNumber + ".pdf",
            ContentDisposition = Rotativa.AspNetCore.Options.ContentDisposition.Inline
        }; 
user1579943
  • 63
  • 1
  • 7
0
Response.AppendHeader("Content-Disposition", new System.Net.Mime.ContentDisposition { Inline = true, FileName = "myCorrectlyNamed.pdf" }.ToString());
return new ViewAsPdf(GetViewName(), "", reportVM); 
David Buck
  • 3,752
  • 35
  • 31
  • 35
  • 4
    Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually of higher quality, and are more likely to attract upvotes. – Mark Rotteveel Mar 31 '20 at 18:03