public ActionResult Pdf() { var rotativaView = new ActionAsPdf(nameof(AreaMissing)) { FileName = "MyPdfFile", PageSize = Rotativa.Options.Size.A4, PageMargins = new Rotativa.Options.Margins(0, 0, 8, 0), CustomSwitches = _setFooter, }; var _byteData = rotativaView.BuildFile(this.ControllerContext); System.IO.File.WriteAllBytes(Server.MapPath("~/Rotativa//test_file.pdf"), _byteData); return(File(_byteData, "application/pdf")); }
public ActionResult SendEmail() { var rotativaView = new ActionAsPdf(nameof(AreaMissing)) { FileName = "MyPdfFile", PageSize = Rotativa.Options.Size.A4, PageMargins = new Rotativa.Options.Margins(0, 0, 8, 0), CustomSwitches = _setFooter, }; var _byteData = rotativaView.BuildFile(this.ControllerContext); //Attach _byteData in the attachment //email.Send(); return(Content("Email successfully sent.")); }
public ActionResult ExportPDF(int?id, bool exportacionDirecta) { if (Session["TiendaId"] == null) { return(RedirectToAction("IniciarSesion", "Login")); } int tiendaId = (int)Session["TiendaId"]; Tienda tienda = db.Tienda.Where(t => t.Id == tiendaId).FirstOrDefault(); Cotizacion cotizacion = tienda.Cotizaciones.Where(c => c.Id == id).FirstOrDefault(); string codigo = cotizacion.Codigo; string nombre = cotizacion.Usuario.NombreCompleto.ToUpper(); string NombreCarpeta = "PDFTiendas"; string NombreVistaPDF = "PreviewExportPDFCotizacion"; string nombreDocumentoPDF = codigo + "_" + nombre + ".pdf"; nombreDocumentoPDF = nombreDocumentoPDF.Replace('|', ' '); ActionAsPdf action = new ActionAsPdf(NombreVistaPDF + "/", new { id = id, tiendaId = tiendaId }) { //FileName = cotizacion.Codigo + ".pdf" FileName = nombreDocumentoPDF, CustomSwitches = "--footer-center \" Página: " + "[page]/[toPage]\"" + " --footer-line --footer-font-size \"10\" --footer-spacing 1 --footer-font-name \"Segoe UI\"" }; string fullPath = ""; string basePath = Path.Combine(System.Web.Hosting.HostingEnvironment.MapPath("~/img/" + NombreCarpeta + "/"), tiendaId + ""); if (!System.IO.File.Exists(basePath)) { Directory.CreateDirectory(basePath); //crear si no existe } int nArchivos = -1; int cFile = 0; string nFile = ""; try { //Se eliminan los archivos en la carpeta si hay muchos string[] fileNames = Directory.GetFiles(basePath); nArchivos = fileNames.Count(); if (fileNames.Count() >= 5) { foreach (string file in fileNames) { string[] separado = file.Split('\\'); nFile = separado.Last(); string path = Path.Combine(System.Web.Hosting.HostingEnvironment.MapPath("~/img/" + NombreCarpeta + "/" + tiendaId), nFile); System.IO.File.Delete(path); cFile++; } } } catch (Exception e) { //ReporteErrores.CrearReporteError(db, "No se pudo borrar PDFs N°Arhivos=" + nArchivos + " numFail=" + cFile + " nombreFail=" + nFile + " MensajeException= " + e.Message + " ExeptionFULL= " + e.ToString()); } if (exportacionDirecta == false) { fullPath = Path.Combine(System.Web.Hosting.HostingEnvironment.MapPath("~/img/" + NombreCarpeta + "/" + tiendaId), nombreDocumentoPDF); try { if (System.IO.File.Exists(fullPath)) { System.IO.File.Delete(fullPath); } var byteArray = action.BuildFile(ControllerContext); var fileStream = new FileStream(fullPath, FileMode.Create, FileAccess.Write); fileStream.Write(byteArray, 0, byteArray.Length); fileStream.Close(); } catch (Exception e) { //ReporteErrores.CrearReporteError(db, "ERROR REEMPLAZANDO PDF MensajeException= " + e.Message + " ExeptionFULL= " + e.ToString()); } } if (exportacionDirecta == true) { return(action); //retorna el ActionAsPdf para descargar } else { //pdf del documento queda almacenado temporalmente //se genera el catalogo para luego enviar ambos por email return(RedirectToAction("RedirectCentral", "Email", new { id = id, tiendaId = tiendaId, nombreDocumento = nombreDocumentoPDF, usuarioId = (int)Session["Id"] })); } }