从Razor View创建PDF并发送附件

时间:2016-08-01 08:01:55

标签: c# asp.net-mvc pdf razor

我正在开发一个asp.net MVC应用程序,并且我要求系统使用附件中的Pdf文件以克里特时间发送电子邮件。我正在使用Quartz发送这些电子邮件。现在我有一个剃刀视图,我想将其转换为PDF并附加在电子邮件中。 但是我在将Cz课程中的razor视图转换为PDF时遇到了问题。

这是我的代码到目前为止

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

尝试创建Pdf

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
        }
}

0 个答案:

没有答案