Create PDF from Razor View and send in attachment
<p>I am working on a asp.net MVC application and I have requirement for sending email on a cretin time by the system with a Pdf file in attachment. I am using Quartz for sending these emails. Now I have a razor view which I want to convert as PDF and attach in email.
but I have problem with converting razor view to PDF in a C# class.</p>

<p>This is my code So far </p>

<pre><code>    public class EmailJob : IJob {
            public void Execute(IJobExecutionContext context)
            {
                PdfResult result = new PdfResult();
                MemoryStream asPdf = result.ExecuteResult( );


                using ( var message = new MailMessage("ikhan.baloch@gmail.com","ikhan.baloch@yahoo.com"))
                {
                    message.Subject = "Test";
                    message.Body = "Test at " + DateTime.Now;
                    message.Attachments.Add(new Attachment(asPdf, String.Format("{0}_DocumentSavingsReport.pdf", "ReportName")));

                    using (SmtpClient client = new SmtpClient {
                        EnableSsl = true,
                        Host = "smtp.gmail.com",
                        Port = 587,
                        Credentials = new NetworkCredential("ikhan.baloch@gmail.com","micronpc")
                    }) 
                    {
                        client.Send(message);
                    }
                }
            }     
}
</code></pre>

<p>Trying to Create Pdf</p>

<pre><code>public class PdfResult : PartialViewResult
{
    // Setting a FileDownloadName downloads the PDF instead of viewing it
    public string FileDownloadName { get; set; }

    public override MemoryStream ExecuteResult(ControllerContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            // Set the model and data
            context.Controller.ViewData.Model = Model;
            ViewData = context.Controller.ViewData;
            TempData = context.Controller.TempData;


            // Get the view name
            if (string.IsNullOrEmpty(ViewName))
            {
                ViewName = context.RouteData.GetRequiredString("action");
            }

            // Get the view
            ViewEngineResult viewEngineResult = null;
            if (View == null)
            {
                viewEngineResult = FindView(context);
                View = viewEngineResult.View;
            }

            // Render the view
            StringBuilder sb = new StringBuilder();
            using (TextWriter tr = new StringWriter(sb))
            {
                ViewContext viewContext = new ViewContext(context, View, ViewData, TempData, tr);
                View.Render(viewContext, tr);
            }
            if (viewEngineResult != null)
            {
                viewEngineResult.ViewEngine.ReleaseView(context, View);
            }

            MemoryStream ms = null;
            // Create a PDF from the rendered view content
            Aspose.Pdf.Generator.Pdf pdf = new Aspose.Pdf.Generator.Pdf();
            using (ms = new MemoryStream(Encoding.UTF8.GetBytes(sb.ToString())))
            {
                pdf.BindXML(ms, null);
            }

            // Save the PDF to the response stream
            using (ms = new MemoryStream())
            {
                pdf.Save(ms);
                FileContentResult result = new FileContentResult(ms.ToArray(), "application/pdf")
                {
                    FileDownloadName = FileDownloadName
                };
                result.ExecuteResult(context);
            }

            return ms
        }
}
</code></pre>