title: generate pdf in template using ASP.net, C#, MVC
body: <p>I am a beginner in MVC and have a requirement to generate PDF of invoices on provided template. After doing a bit of googling, now I am able to generate a pdf but not in Template. Can any body help me on this. I am writing my code here below:</p>
  
  <pre><code>public ActionResult pdfStatement(string InvoiceNumber)
  {
      InvoiceNumber = InvoiceNumber.Trim();
      ObjectParameter[] parameters = new ObjectParameter[1];
      parameters[0] = new ObjectParameter("InvoiceNumber", InvoiceNumber);
      var statementResult = _db.ExecuteFunction&lt;Models.Statement&gt;("uspInvoiceStatement", parameters);
      Models.Statement statement = statementResult.SingleOrDefault();
  
      return ViewPdf("Invoice", "pdfStatement", statement);
  }
  </code></pre>
  
  <hr>
  
  <pre><code>public class PdfViewController : Controller
  {
      private readonly HtmlViewRenderer htmlViewRenderer;
      private readonly StandardPdfRenderer standardPdfRenderer;
  
      public PdfViewController()
      {
          this.htmlViewRenderer = new HtmlViewRenderer();
          this.standardPdfRenderer = new StandardPdfRenderer();
      }
  
      protected ActionResult ViewPdf(string pageTitle, string viewName, object model)
      {
          // Render the view html to a string.
          string htmlText = this.htmlViewRenderer.RenderViewToString(this, viewName, model);
          // Let the html be rendered into a PDF document through iTextSharp.
          byte[] buffer = standardPdfRenderer.Render(htmlText, pageTitle);
  
          // Return the PDF as a binary stream to the client.
          return new BinaryContentResult(buffer, "application/pdf");
      }
  }
  </code></pre>
  
  <hr>
  
  <pre><code>public class BinaryContentResult : ActionResult
  {
      private readonly string contentType;
      private readonly byte[] contentBytes;
  
      public BinaryContentResult(byte[] contentBytes, string contentType)
      {
          this.contentBytes = contentBytes;
          this.contentType = contentType;
      }
  
      public override void ExecuteResult(ControllerContext context)
      {
          var response = context.HttpContext.Response;
          response.Clear();
          response.Cache.SetCacheability(HttpCacheability.Public);
          response.ContentType = this.contentType;
  
          using (var stream = new MemoryStream(this.contentBytes))
          {
              stream.WriteTo(response.OutputStream);
              stream.Flush();
          }
      }
  }
  </code></pre>