public override void ExecuteResult(ControllerContext context)
{
if (context == null)
throw new ArgumentNullException("context");
var response = PrepareResponse(context.HttpContext.Response);
if (WkhtmltopdfPath == string.Empty)
WkhtmltopdfPath = HttpContext.Current.Server.MapPath("~/Rotativa");
context.Controller.ViewData.Model = Model;
// use action name if the view name was not provided
if (string.IsNullOrEmpty(ViewName))
ViewName = context.RouteData.GetRequiredString("action");
using (var sw = new StringWriter())
{
ViewEngineResult viewResult = ViewEngines.Engines.FindView(context, ViewName, MasterName);
// view not found, throw an exception with searched locations
if (viewResult.View == null)
{
StringBuilder locations = new StringBuilder();
locations.AppendLine();
foreach (string location in viewResult.SearchedLocations)
{
locations.AppendLine(location);
}
throw new InvalidOperationException(string.Format("The view '{0}' or its master was not found, searched locations: {1}", ViewName, locations));
}
var viewContext = new ViewContext(context, viewResult.View, context.Controller.ViewData, context.Controller.TempData, sw);
viewResult.View.Render(viewContext, sw);
StringBuilder html = sw.GetStringBuilder();
// replace href and src attributes with full URLs
string baseUrl = string.Format("{0}://{1}", HttpContext.Current.Request.Url.Scheme, HttpContext.Current.Request.Url.Authority);
html.Replace(" href=\"/", string.Format(" href=\"{0}/", baseUrl));
html.Replace(" src=\"/", string.Format(" src=\"{0}/", baseUrl));
var fileContent = WkhtmltopdfDriver.ConvertHtml(WkhtmltopdfPath, GetConvertOptions(), html.ToString());
response.OutputStream.Write(fileContent, 0, fileContent.Length);
}
}