How to return PDF to browser in MVC?

How do I get the controller to return the pdf file to the web browser?

Running this code does open Acrobat but I get an error message “The file is damaged and might not be fixed”

Return a FileContentResult. The last line in your controller action would be something like:

return File("Chap0101.pdf", "application/pdf");

If you are creating this PDF dynamically, it might be better to use a MemoryStream, and create the document in memory instead of conserving to file. The code would be something like:

For the file to be opened straight in the browser instead of being downloaded
Response.AppendHeader("content-disposition", "inline; filename=file.pdf");
return new FileStreamResult(stream, "application/pdf")

If you return a FileResult from your action method, and utilize the File() extension technique on the controller, doing exactly what you desire is quite simple. There are overrides on the File() approach that will take the binary contents of the file, the path to the file, or a Stream.
public FileResult DownloadFile()
return File("path\\to\\pdf.pdf", "application/pdf");

I have actually faced similar problems and I’ve stumbled accross a service. I used 2 posts, one from stack that shows the method to return for download and another one that shows a working option for ItextSharp and MVC.

I know this concern is old however I believed I would share this as I might not find anything similar.

I wished to create my views/models as typical using Razor and have them rendered as Pdfs.

This strategy I had command over the pdf presentation making use of regular html result instead of finding out the most effective means to create the documents utilizing iTextSharp.

You would ordinarily perform a Response.Flush complied with by a Response.Close, but also for some main reason the iTextSharp collection does certainly not appear to like this. The records performs deficient with and also Adobe assumes the PDF is unethical. Forget the Response.Close feature as well as view if your end results are far better:


Leave a Reply

Your email address will not be published. Required fields are marked *