public static MemoryStream CreatePdf(CertificateVM certificate) { string templatePath = certTemplateName + certificate.TemplateName; if (certificate.CertificateID == null) { return(null); } string pdfTemplate = HttpContext.Current.Server.MapPath(templatePath); MemoryStream workStream = new MemoryStream(); using (PdfReader pdfReader = new PdfReader(pdfTemplate)) { using (PdfStamper pdfStamper = new PdfStamper(pdfReader, workStream)) { AcroFields pdfFormFields = pdfStamper.AcroFields; pdfFormFields.SetField("presentedTo", "Presented To"); pdfFormFields.SetField("userName", certificate.UserFirstName); if (!certificate.SubTopicDesc.StartsWith("Percentage")) { pdfFormFields.SetField("for", "For"); } pdfFormFields.SetField("subTopicDesc", certificate.SubTopicDesc); pdfFormFields.SetField("doc", certificate.DateOfCompletion + "\n"); pdfFormFields.SetField("signatureBy", ""); pdfFormFields.SetField("companyName", "iPracticeMath.com"); pdfStamper.FormFlattening = true; } } return(workStream); }
public async Task <IActionResult> EmailCertificate([FromRoute] int regId, [FromServices] CertificatePdfRenderer writer, [FromServices] StandardEmailSender emailSender) { var c = await _certificatesService.GetForRegistrationAsync(regId); var result = await writer.RenderAsync(CertificateVM.From(c)); var memoryStream = new MemoryStream(); await result.CopyToAsync(memoryStream); var emailMessage = new EmailMessage { Email = c.RecipientEmail, Subject = $"Kursbevis for {c.Title}", Message = "Her er kursbeviset! Gratulere!", Attachment = new Attachment { Filename = "kursbevis.pdf", Bytes = memoryStream.ToArray(), ContentType = "application/pdf" } }; await emailSender.SendStandardEmailAsync(emailMessage); return(Ok()); }
public async Task <Stream> RenderAsync(CertificateVM vm) { var html = await _renderService.RenderViewToStringAsync(Template, vm); return(await _pdfRenderService.RenderHtmlAsync(html, new PdfRenderOptions { Format = "A4", Scale = 0.8f })); }
public async Task <IActionResult> ViewCertificate( [FromRoute] int id, [FromServices] ICertificatesService certificatesService) { var certificate = await certificatesService.GetAsync(id); if (certificate == null) { return(NotFound()); } return(View("Templates/Certificates/CourseCertificate", CertificateVM.From(certificate))); }
public async Task <IActionResult> DownloadCertificate( [FromServices] CertificatePdfRenderer writer, [FromServices] ICertificatesService certificatesService, [FromRoute] int id) { var certificate = await certificatesService.GetAsync(id); if (certificate == null) { return(NotFound()); } var stream = await writer.RenderAsync(CertificateVM.From(certificate)); MemoryStream memoryStream = new MemoryStream(); await stream.CopyToAsync(memoryStream); return(File(memoryStream.ToArray(), "application/pdf")); }
public async Task <IActionResult> GenerateCertificatesAndSendEmails([FromRoute] int eventId, [FromServices] CertificatePdfRenderer writer, [FromServices] StandardEmailSender emailSender) { var certificates = await _certificatesService.CreateCertificatesForEvent(eventId); foreach (var certificate in certificates) { var result = await writer.RenderAsync(CertificateVM.From(certificate)); var memoryStream = new MemoryStream(); await result.CopyToAsync(memoryStream); await emailSender.SendStandardEmailAsync(new EmailMessage { Email = certificate.RecipientEmail, Subject = $"Kursbevis for {certificate.Title}", Message = "Her er kursbeviset! Gratulere!", Attachment = new Attachment { Filename = "kursbevis.pdf", Bytes = memoryStream.ToArray() } }); } return(Ok()); }