Asp.Net MVC how to get view to generate PDF

前端 未结 8 1602
醉酒成梦
醉酒成梦 2020-11-28 19:09

I would like to call an action on a controller. Have the controller get the data from the model. The view then runs and generates a PDF. The only example I have found i

8条回答
  •  广开言路
    2020-11-28 19:47

    This is an old question but one that's still relevant and I thought I'd just share what I've implemented which works well.

    1. Install NuGet package TuesPechkin - a fork in the Pechkin library based on WkHtmlToPdf that uses a Webkit engine to convert HTML pages to PDF.

    2. Write a little helper to read a view and convert it to an HTML string (mvcContext is this.HttpContext). The replace is optional of course!:

      public static string RenderViewToString(HttpContextBase mvcContext, string area, string controllerName, string viewName, object model)
      {
          var context = System.Web.HttpContext.Current;
          var contextBase = mvcContext;
          var routeData = new RouteData();
          if (area == null) area = "";
      
          routeData.DataTokens.Add("area", area);
      
          routeData.Values.Add("controller", controllerName);
      
          var controllerContext = new ControllerContext(contextBase,
                                                  routeData,
                                                  new EmptyController());
      
          var razorViewEngine = new RazorViewEngine();
          var razorViewResult = razorViewEngine.FindView(controllerContext,
                                                      viewName,
                                                      "",
                                                  false);
      
          var writer = new StringWriter();
          var viewContext = new ViewContext(controllerContext,
                                      razorViewResult.View,
                                      new ViewDataDictionary(model),
                                      new TempDataDictionary(),
                                      writer);
          razorViewResult.View.Render(viewContext, writer);
      
          string hostAddress = context.Request.Url.Scheme + "://" + context.Request.Url.Authority;
      
          return writer.ToString()
                       .Replace("src=\"/", "src=\"" + hostAddress + "/")
                       .Replace("

    The hard work of the above were from here: http://wouterdekort.blogspot.co.uk/2012/10/rendering-aspnet-mvc-view-to-string-in.html?showComment=1414603363455#c7863520150405064571

    1. Create an MVC Action to generate the document

      public ActionResult DownloadPDF(long CentreID)
      {
          var model = GetModel()
      
          IPechkin converter = Factory.Create();
          byte[] result = converter.Convert(Helpers.PDF.RenderViewToString(this.HttpContext, "area", "controller", "action", model);
          MemoryStream outputStream = new MemoryStream();
          outputStream.Write(result, 0, result.Length);
          outputStream.Position = 0;
      
          return File(outputStream, "application/pdf", "filename.pdf");
      }
      

提交回复
热议问题