public async Task <IActionResult> ViewOnWebPDF(int id) { try { var invoiceWithAll = await _repositoryWrapper.Invoice.GetInvoiceWihtAllDetailsForInvoiceGeneration(id); // Add Application user to get access for email data ApplicationUser landlordAspUser = invoiceWithAll.Rent.Landlord.ApplicationUser; ApplicationUser tenantAspUser = invoiceWithAll.Rent.Tenant.ApplicationUser; Invoice invoice = _mapper.Map <Invoice>(invoiceWithAll); Rent rent = _mapper.Map <Rent>(invoiceWithAll.Rent); Tenant tenant = _mapper.Map <Tenant>(invoiceWithAll.Rent.Tenant); Property property = _mapper.Map <Property>(invoiceWithAll.Rent.Property); Landlord landlord = _mapper.Map <Landlord>(invoiceWithAll.Rent.Landlord); Photo photo = _mapper.Map <Photo>(invoiceWithAll.State.Photo); Rate rate = _mapper.Map <Rate>(invoiceWithAll.Rent.Property.Rate); var globalSettings = new GlobalSettings { ColorMode = ColorMode.Color, Orientation = Orientation.Portrait, PaperSize = PaperKind.A4, Margins = new MarginSettings { Top = 10 }, DocumentTitle = "Invoice title", }; InvoiceFile invoiceGenerated = TemplateGenerator.GetInvoiceHTMLString(landlordAspUser, tenantAspUser, invoice, tenant, property, landlord, rent); var objectSettings = new ObjectSettings { PagesCount = true, HtmlContent = invoiceGenerated.getInvoiceString(), WebSettings = { DefaultEncoding = "utf-8", UserStyleSheet = SystemRecognizer.GetCssFileLocation() }, HeaderSettings = { FontName = "Arial", FontSize = 9, Right = "Page [page] of [toPage]", Line = true }, FooterSettings = { FontName = "Arial", FontSize = 9, Line = true, Center = "Report Footer" } }; var pdf = new HtmlToPdfDocument() { GlobalSettings = globalSettings, Objects = { objectSettings } }; //_converter.Convert(pdf); var file = _converter.Convert(pdf); //return Ok("Successfully created PDF document."); return(File(file, "application/pdf", invoiceGenerated.getInvoiceFileName())); } catch (Exception e) { _logger.LogError($"Something went wrong inside ViewOnWebPDF(id) action: {e.ToString()}"); return(StatusCode(500, e.Message)); } }
public async Task <IActionResult> SendMailToTenantWithPDF(int id) { try { var invoiceWithAll = await _repositoryWrapper.Invoice.GetInvoiceWihtAllDetailsForInvoiceGeneration(id); // Add Application user to get access for email data ApplicationUser landlordAspUser = invoiceWithAll.Rent.Landlord.ApplicationUser; ApplicationUser tenantAspUser = invoiceWithAll.Rent.Tenant.ApplicationUser; Invoice invoice = _mapper.Map <Invoice>(invoiceWithAll); Rent rent = _mapper.Map <Rent>(invoiceWithAll.Rent); Tenant tenant = _mapper.Map <Tenant>(invoiceWithAll.Rent.Tenant); Property property = _mapper.Map <Property>(invoiceWithAll.Rent.Property); Landlord landlord = _mapper.Map <Landlord>(invoiceWithAll.Rent.Landlord); Photo photo = _mapper.Map <Photo>(invoiceWithAll.State.Photo); Rate rate = _mapper.Map <Rate>(invoiceWithAll.Rent.Property.Rate); var globalSettings = new GlobalSettings { ColorMode = ColorMode.Color, Orientation = Orientation.Portrait, PaperSize = PaperKind.A4, Margins = new MarginSettings { Top = 10 }, DocumentTitle = "Invoice title", //Out = SystemRecognizer.GetGeneratedInvoiceFileLocation() // this line will generate an invoice file on in InvoiceGeneratior/invoices folder but wont let generate proper attachment!! }; InvoiceFile invoiceGenerated = TemplateGenerator.GetInvoiceHTMLString(landlordAspUser, tenantAspUser, invoice, tenant, property, landlord, rent); var objectSettings = new ObjectSettings { PagesCount = true, HtmlContent = invoiceGenerated.getInvoiceString(), WebSettings = { DefaultEncoding = "utf-8", UserStyleSheet = SystemRecognizer.GetCssFileLocation() }, HeaderSettings = { FontName = "Arial", FontSize = 9, Right = "Page [page] of [toPage]", Line = true }, FooterSettings = { FontName = "Arial", FontSize = 9, Line = true, Center = "Report Footer" } }; var pdf = new HtmlToPdfDocument() { GlobalSettings = globalSettings, Objects = { objectSettings } }; var invoiceFile = _converter.Convert(pdf); var message = new Email( new string[] { /*tenant.Email,*/ "*****@*****.**" /*, "*****@*****.**",*/ }, $"Nowa Faktura od: {landlord.CompanyName}", $"Nowa faktura do zaplaty za mieszkanie {property.FlatLabel} " + $"{property.Address.City} " + $"{property.Address.Street} " + $"{property.Address.BuildingNumber}/{property.Address.FlatNumber}. Prosimy o terminową wpłatę na konto: {landlord.BankAccount}. ", new List <byte[]> { invoiceFile }, new List <string> { invoiceGenerated.getInvoiceFileName().Replace('/', '-') } ); await _emailEmmiter.SendMailAsync(message); return(Ok()); } catch (Exception e) { _logger.LogError($"Something went wrong inside SendMailToTenantWithPDF(id) action: {e.ToString()}"); return(StatusCode(500, e.Message)); } }