public JsonResult Index(JoePdf model) { #if !DEBUG try { #endif _pdfFiller.FillInPdfAndEmail(model, Server.MapPath("~/Content/2015 ASE ORDER WORKSHEET FORM.pdf")); return Json(new { Success = true, }); #if !DEBUG } catch (Exception ex) { return Json (new { Success = false, Error = new { Message = ex.Message, InnerMessage = ex.InnerException == null ? string.Empty : ex.InnerException.Message }, }); } #endif }
private string FillPdf(JoePdf model, string pdfPath) { if (pdfPath == null) throw new ArgumentNullException("pdfPath"); var targetPdfPath = Path.Combine(Path.GetTempPath(), string.Format("{0}{1}{2}", Path.GetFileNameWithoutExtension(pdfPath), _random.Next(10000), Path.GetExtension(pdfPath))); var fieldMappings = new Dictionary<string, string> { {"DATE", FormatDate(model.Date)}, {"DATE NEEDED", FormatDate(model.DateNeeded)}, {"CLIENT NAME", model.ClientName}, {"COMPANY NAME", model.CompanyName}, {"EMBROIDERY",FormatBool(model.Embroidery)}, {"SCREEN PRINTING", FormatBool(model.ScreenPrinting)}, {"PHONE", model.Phone}, {"EMAIL", model.Email}, {"ARTWORK RECIEVED", FormatBool(model.ArtworkRecieved)}, {"ARTWORK LOCATION", model.ArtworkLocation}, {"Order Submitted by", "Joe and Cami Anderson"}, {"Date Submitted", DateTime.Now.ToString("MM/dd/yyyy")}, }; if (model.ItemOrders != null) { for (int i = 0; i < model.ItemOrders.Length; i++) { var item = model.ItemOrders[i]; var num = i == 0 ? "" : "_" + (i + 1); fieldMappings.Add("Style" + num, item.Style); fieldMappings.Add("Item Color" + num, item.ItemColor); if (item.SizeOptions != null) { foreach (var size in item.SizeOptions) { var key = size.Size + "Row1" + num; if (!fieldMappings.ContainsKey(key)) fieldMappings.Add(key, size.Number.ToString()); } } } } if (model.ColorOptions != null) { foreach (var color in model.ColorOptions.Where(opt => !string.IsNullOrEmpty(opt.Location) && !string.IsNullOrEmpty(opt.Position))) { var key = color.Location.ToUpper().Replace(" ", "_") + "_" + color.Position.ToUpper(); fieldMappings.Add(key, "X"); fieldMappings.Add(key + "_COLORS", color.NumberOfColors.ToString()); } } _manipPdf.FillInFields(pdfPath, fieldMappings, targetPdfPath); return targetPdfPath; }
public void FillInPdfAndEmail(JoePdf model, string pdfPath) { #if !DEBUG try { #endif _uow.SiteLogs.Add(new DbContexts.Models.Site.SiteLog { DateOfLog = DateTime.UtcNow, LogType = DbContexts.Models.Site.LogType.Log, Log = model.ToJson(), }); _uow.SaveChanges(); var message = new System.Net.Mail.MailMessage { Subject = "Joe and Cami Anderson Order " + DateTime.Now, Body = "Auto Generated through: http://cnjprinting.com/ Please direct any questions to Joe", }; #if DEBUG message.CC.Add(new MailAddress("*****@*****.**", "Ben Anderson")); #else message.CC.Add(new MailAddress("*****@*****.**", "Joseph Anderson")); message.CC.Add(new MailAddress("*****@*****.**", "Cami Anderson")); #endif Guid guid; if (model.FileGuid != null && Guid.TryParse(model.FileGuid.Replace("\"", ""), out guid)) { model.ArtworkRecieved = true; var dbFile = _uow.DbFiles.Find(file => file.Key == guid).FirstOrDefault(); if (dbFile == null) throw new InvalidOperationException("No file found with key " + model.FileGuid); var fileRef = _file.GetFile(dbFile.FileName); message.Attachments.Add(new Attachment(new MemoryStream(fileRef.FileContent), dbFile.OriginalFileName)); } else { model.ArtworkRecieved = false; } var targetPdfPath = FillPdf(model, pdfPath); message.Attachments.Add(new Attachment(targetPdfPath)); _email.SendEmail(message); #if !DEBUG } catch (Exception ex) { _uow.SiteLogs.Add(new DbContexts.Models.Site.SiteLog { DateOfLog = DateTime.UtcNow, Log = ex.ToString(), LogType = DbContexts.Models.Site.LogType.Error }); _uow.SaveChanges(); throw; } #endif }