public void SavePdfLocally(string savePath, PdfDocument pdf, CatContractViewModel vm, string filename) { savePath += filename; //Save to Local Directory and open file try { pdf.Save(savePath); //Uncomment to open after saving locally //Process.Start(savePath); } catch (Exception e) { Console.WriteLine(e.Message); } }
public ActionResult CatContract(string submit, CatContractViewModel vm) { if (ModelState.IsValid) { string savePath = null; //Where to save locally; null to disable string sourcePath = @"~/App_data/Cat Contract.pdf"; //PDF target document string JSONPath = @"~/App_Data/Cat Contract Coordinates.json"; //JSON coordinates of where to print on the new PDF //Build Filename string ContractSignerName = vm.OwnerName != null?vm.OwnerName.Replace(" ", "") : "Unknown"; string pdfName = "CatContract-" + ContractSignerName + "-" + DateTime.Today.ToString("MMddyyyy") + ".pdf"; //Create the document and save it to the specified location PdfDocument newCatContract = PrintPdfFromViewModel(vm, savePath, sourcePath, JSONPath); if (savePath != null) { SavePdfLocally(savePath, newCatContract, vm, pdfName); } if (submit == "Submit and Email") { try { EmailAgreement(newCatContract, vm.Email, pdfName); } catch (Exception e) { ModelState.AddModelError("empty", e.Message + " Please try downloading instead"); return(View()); } ViewBag.Email = vm.Email; return(RedirectToAction("success", new { email = vm.Email })); } MemoryStream stream = new MemoryStream(); newCatContract.Save(stream, false); return(File(stream, "application/pdf", pdfName)); } return(View(vm)); }