I'm doing a PDF export where the content of the document is the html of a Rendered View.
How do I do this?
I'm doing a PDF export where the content of the document is the html of a Rendered View.
How do I do this?
I've used the solution from this other question (in English):
public string RenderRazorViewToString(string viewName, object model)
{
ViewData.Model = model;
using (var sw = new StringWriter())
{
var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext,
viewName);
var viewContext = new ViewContext(ControllerContext, viewResult.View,
ViewData, TempData, sw);
viewResult.View.Render(viewContext, sw);
viewResult.ViewEngine.ReleaseView(ControllerContext, viewResult.View);
return sw.GetStringBuilder().ToString();
}
}
public ActionResult RenderViewToString()
{
var model = new MeuModelo()
{
Nome = "William John",
Endereco = "123, rue des Canadiens",
Telefone = "(555) 555-7777"
};
string html = RenderViewToString(
"~/views/Etiqueta.cshtml",
model);
CreatePdf(html);
return View();
}
How it works: