2

What is the best way to output an MVC page as PDF and switching the master page to a different master page?

I was thinking I could add a parameter to the query string like format=pdf and set some sort of filtering that capture the output and transform it to a pdf.

Using classic ASP.NET I did that in an HttpModule.

What is the best way of doing it in MVC?

I have to be able to "print" all the pages in my application, so a FileResult controller method would not work. I need something generic that can work with every url adding that specific query string parameter

smnbss
  • 1,031
  • 1
  • 10
  • 21

1 Answers1

2

Write a FileResult controller method.

How to create file and return it via FileResult in ASP.NET MVC?

As part of the return result you set the MIME type.

Community
  • 1
  • 1
Kenoyer130
  • 6,874
  • 9
  • 51
  • 73
  • I have to be able to "print" all the pages in my application, so a FileResult controller method would not work. I need something generic that can work with every url adding that specific query string parameter – smnbss May 24 '11 at 10:42
  • Does this link help? http://stackoverflow.com/questions/1324597/how-to-render-an-asp-net-mvc-view-in-pdf-format – Kenoyer130 May 24 '11 at 10:52
  • sort of, but that would require to have OnResultExecuting and OnResultExecuted on every Controller... I was hoping for a cross cutting, generic way of doing it. Something like an Action Filter, that is executed only if there's specific parameter on the query string, that swap the master page to the printable one and render the page as pdf – smnbss May 24 '11 at 12:15
  • well in mvc3 there are global filters so you can snap it in global.asax, check for a parameter in it and if there - do what a man got to do. – Alexander Taran May 24 '11 at 14:53
  • yes, I wanted to know if somebody implemented it to see if there are correlated problems but I guess I'll have to try and see. thanks – smnbss May 24 '11 at 15:59