ASP.NET MVC 2 View (ViewModel) -> MS Word or PDF Generation?
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 =开发者_StackOverflow社区 searchString;
Employees = employees;
Organizations = organizations;
PageInfo = pageInfo;
}
}
I'm having trouble kinda connecting the two - to stream the view using my viewmodel to pdf
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.
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.
I'm a bit late to this, but from what I can see:
public ActionResult DetailedReportWord()
{
string url = "DetailedReport";
return new WordActionResult(url, "detailed-report.doc");
}
Your url is pointing back to the same Action, it should be pointing at your "StartSearch" action as that is the one that produces the HTML you are wanting to be opened by Word. It needs to be a full URL too I think.
I am trying to use this method too, I found it from your question! I'm having issues passing over the authorised credentials in the WebRequest.Create(Url) for the source View however.
精彩评论