Tutorial :Asp.Net MVC how to get view to generate PDF



Question:

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 is in an article by Lou http://whereslou.com/2009/04/12/returning-pdfs-from-an-aspnet-mvc-action. His code is very elegant. The view is using ITextSharp to generate the PDF. The only downside is his example uses the Spark View Engine. Is there a way to do a similar thing with the standard Microsoft view engine?


Solution:1

I use iTextSharp to generate dynamic PDF's in MVC. All you need to do is put your PDF into a Stream object and then your ActionResult return a FileStreamResult. I also set the content-disposition so the user can download it.

  public FileStreamResult PDFGenerator()  {      Stream fileStream = GeneratePDF();        HttpContext.Response.AddHeader("content-disposition",       "attachment; filename=form.pdf");        return new FileStreamResult(fileStream, "application/pdf");  }  

I also have code that enables me to take a template PDF, write text and images to it etc (if you wanted to do that).

  • Note: you must set the Stream position to 0.
  private Stream GeneratePDF()  {      //create your pdf and put it into the stream... pdf variable below      //comes from a class I use to write content to PDF files        MemoryStream ms = new MemoryStream();        byte[] byteInfo = pdf.Output();      ms.Write(byteInfo, 0, byteInfo.Length);      ms.Position = 0;        return ms;  }  


Solution:2

our final answer to this problem was to use Rotativa.

It wraps up the WKhtmltopdf.exe like some of the other solutions, but it's by far the easiest to use that I have found

I went and up voted all the other answers that also solve the problem well, but this is what we used to solve the problem posed in the question above. It is different from the other answers.

Here is a Rotativa Tutorial.

after you install it, this is all your need

public ActionResult PrintInvoice(int invoiceId)  {    return new ActionAsPdf(                   "Invoice",                    new { invoiceId= invoiceId })                    { FileName = "Invoice.pdf" };  }  

Very Very simple.


Solution:3

I also came across this http://www.codeproject.com/Articles/260470/PDF-reporting-using-ASP-NET-MVC3. It's easy and swift, and fits well with MVC.

However, the only downside so far is that it's not quite flexible you want to have a decent layout, for example, you don't have much control with table, and cell borders through html. It sort of supports force new page, but you will have to apply a patch to iTextsharp.


Solution:4

Creating layout in html and printing afterwards into pdf is the fastest way.

Html into pdf conversion is provided by phantomjs, wkhtmltopdf or jsreport

jsreport provides direct integration with asp.net mvc views, where you can just mark controller action with attribute and it will print pdf instead of html for you.

More on this blog post

Disclaimer: I am the author of jsreport


Solution:5

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("<link href=\"/", "<link href=\"" + hostAddress + "/");                           }    class EmptyController : ControllerBase  {      protected override void ExecuteCore() { }  }  

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");  }  


Solution:6

I just used wkhtmltopdf, to create the layout in html and afterwards, i convert it to pdf.

Easy, customizable, awesome as hell :)


Solution:7

Very Late Reply, but I found that the following URL helped me get my results quickly :

(Ensure that you reference to the iTextSharp DLL by making use of the Nuget Packages)

https://www.aspsnippets.com/Articles/Export-Grid-Html-Table-data-from-database-to-PDF-file-using-iTextSharp-in-ASPNet-MVC.aspx

EDIT This is the code I used to make the table look a little different(This is landscape as well:

public string GetCssForPdf()          {              string css = "";                css = "th, td" +                     "{" +                         "font-family:Arial; font-size:10px" +                      "}";                return css;          }    [HttpPost]          [ValidateInput(false)]          public FileResult Export(string GridHtml)          {              string webgridstyle = GetCssForPdf();                string exportData = String.Format("<html><body>{0}{1}</body></html>", "<style>" + webgridstyle + "</style>", GridHtml);              var bytes = System.Text.Encoding.UTF8.GetBytes(exportData);                using (var input = new MemoryStream(bytes))              {                  var output = new MemoryStream();                  var document = new iTextSharp.text.Document(PageSize.A4, 50, 50, 50, 50);                  var writer = PdfWriter.GetInstance(document, output);                    document.SetPageSize(iTextSharp.text.PageSize.A4.Rotate());                  Font headerFont = FontFactory.GetFont("Verdana", 10);                  Font rowfont = FontFactory.GetFont("Verdana", 10);                    writer.CloseStream = false;                  document.Open();                    var xmlWorker = iTextSharp.tool.xml.XMLWorkerHelper.GetInstance();                  xmlWorker.ParseXHtml(writer, document, input, System.Text.Encoding.UTF8);                  document.Close();                  output.Position = 0;                    return File(output, "application/pdf", "Pipeline_Report.pdf");                    //return new FileStreamResult(output, "application/pdf");              }          }  

Hope this helps someone else as well.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »