views:

52

answers:

2

Id like to have my mvc 2 app generating reports in both MS Word and PDF formats....currently working on Word. I found this:

http://www.revium.com.au/articles/sandbox/aspnet-mvc-convert-view-to-word-document/

Which i think basically streams the view output from a controller action to a word document....

public ActionResult DetailedReport()  
    {            
        //[...]    
        return View();  
    }  


public ActionResult DetailedReportWord()  
    {  
        string url = "DetailedReport";  
        return new WordActionResult(url, "detailed-report.doc");  
    }  

And heres the class that extends ActionResult

public class WordActionResult : ActionResult   
{   private string Url { get; set;}          
private string FileName { get; set;}  


public WordActionResult(string url, string fileName)  
{  
    Url = url;  
    FileName = fileName;  
}  

public override void ExecuteResult(ControllerContext context)  
{  
    var curContext = HttpContext.Current;  
    curContext.Response.Clear();  
    curContext.Response.AddHeader("content-disposition", "attachment;filename=" + FileName);  
    curContext.Response.Charset = "";  
    curContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);  
    curContext.Response.ContentType = "application/ms-word";  

    var wreq = (HttpWebRequest) WebRequest.Create(Url);  
    var wres = (HttpWebResponse) wreq.GetResponse();  
    var s = wres.GetResponseStream();  
    var sr = new StreamReader(s, Encoding.ASCII);  

    curContext.Response.Write(sr.ReadToEnd());  
    curContext.Response.End();  
}  

}

Which looks pretty good - but my problem is that my view renders from a ViewModel, here is the Action

[HttpPost]  
    public ActionResult StartSearch(SearchResultsViewModel model)  
    {  
        SearchResultsViewModel resultsViewModel = _searchService.Search(model.Search, 1, PAGE_SIZE);  
        return View("Index", resultsViewModel);  
    }  

and here is my model:

public class SearchResultsViewModel  
{  
[SearchWordLimit]  
public string Search { get; set; }  

public IEnumerable<Employee> Employees { get; private set; }  
public IEnumerable<Organization> Organizations { get; private set; }  
public PageInfo PageInfo { get; private set;}  

public SearchResultsViewModel()  
{  
}  

public SearchResultsViewModel(string searchString, IEnumerable<Employee> employees, IEnumerable<Organization> organizations, PageInfo pageInfo)  
{  
    Search = searchString;  
    Employees = employees;  
    Organizations = organizations;  
    PageInfo = pageInfo;  
}  

}

I'm having trouble kinda connecting the two - to stream the view using my viewmodel to pdf

Any help would be appreciated!

+2  A: 

There's nothing out of the box in ASP.NET MVC that allows you to build a PDF or Word file from a POCO class. You have to build it manually or using a third party library. Once you have done this you could easily write the bytes to the response stream:

public ActionResult SomeAction(SearchResultsViewModel model)
{
    byte[] pdf = GeneratePdfForModel(model);
    return File(pdf, "application/pdf");
}

The GeneratePdfForModel method will of course be specific on what library/API you are using to generate the document.

Darin Dimitrov
A: 

The trick is to generate the PDF file from the MS-Word file. You most likely need a 3rd party component for this.

If you don't need perfect conversion fidelity, just something that is 'good enough', then try Aspose.Words. If you need perfect conversion Fidelity then try a product I have worked on, it allows the conversion of all typical MS-Office types to PDF format using a web services interface.

Muhimbi