Search code examples
c#asp.netasp.net-mvcasp.net-mvc-4razorpdf

PDF force download as opposed to open in browser


I am using RazorPDF and I would like to force download the PDF as opposed to open in browser tab. How do I do this? Thanks

public ActionResult Index()
{
    return View();
}

[HttpPost]
public ActionResult Index(string Id)
{
    return RedirectToAction("Pdf");
}

public PdfResult Pdf()
{
    // With no Model and default view name.  Pdf is always the default view name
    return new PdfResult();
}

Solution

  • Try adding the content-disposition header before returning the PDFResult object.

    public PdfResult Pdf()
    {
      Response.AddHeader("content-disposition", "attachment; filename=YourSanitazedFileName.pdf");
    
      // With no Model and default view name.  Pdf is always the default view name
      return new PdfResult();
    }