public async Task <FileResult> PrintOrder([FromRoute] int orderId)
        {
            var order = await _context.Order.AsNoTracking()
                        .Include(o => o.OrderDetail)
                        .ThenInclude(o => o.Product)
                        .Include(t => t.OrderTax)
                        .ThenInclude(t => t.Tax)
                        .Include(o => o.OrderPayment)
                        .Include(o => o.Customer)
                        .Include(l => l.Location)
                        .SingleOrDefaultAsync(m => m.OrderId == orderId);

            var user = await _context.Users.FirstOrDefaultAsync(u => u.Id == order.CreatedByUserId);

            order.CreatedByUserName = user.GivenName;

            var globalSettings = new GlobalSettings
            {
                ColorMode   = ColorMode.Color,
                Orientation = Orientation.Portrait,
                PaperSize   = PaperKind.A4,
                Margins     = new MarginSettings {
                    Top = 10
                },
                DocumentTitle = $"Order {order.OrderId}",
                // Out = @"C:\PDFCreator\Employee_Report.pdf"
            };

            var objectSettings = new ObjectSettings
            {
                PagesCount  = true,
                HtmlContent = OrderTemplateGenerator.GetHtmlString(order, true),
                WebSettings = { DefaultEncoding = "utf-8", UserStyleSheet = Path.Combine(Directory.GetCurrentDirectory(), "assets", "invoice.css") },
                // HeaderSettings = { FontName = "Arial", FontSize = 9, Right = "Page [page] of [toPage]", Line = true },
                // FooterSettings = { FontName = "Arial", FontSize = 9, Line = true, Center = "Page [page] of [toPage]" }
            };

            var pdf = new HtmlToPdfDocument()
            {
                GlobalSettings = globalSettings,
                Objects        = { objectSettings }
            };

            // _converter.Convert(pdf);
            var file = _converter.Convert(pdf);
            FileContentResult result = new FileContentResult(file, "application/pdf")
            {
                FileDownloadName = $"Order-{order.OrderId}.pdf"
            };

            return(result);
        }
        public async Task <IActionResult> EmailOrder([FromRoute] int orderId, [FromQuery] string email)
        {
            var order = await _context.Order.AsNoTracking()
                        .Include(o => o.OrderDetail)
                        .ThenInclude(o => o.Product)
                        .Include(t => t.OrderTax)
                        .ThenInclude(t => t.Tax)
                        .Include(o => o.OrderPayment)
                        .Include(o => o.Customer)
                        .Include(l => l.Location)
                        .SingleOrDefaultAsync(m => m.OrderId == orderId);

            var globalSettings = new GlobalSettings
            {
                ColorMode   = ColorMode.Color,
                Orientation = Orientation.Portrait,
                PaperSize   = PaperKind.A4,
                Margins     = new MarginSettings {
                    Top = 10
                },
                DocumentTitle = $"Order {order.OrderId}",
                // Out = @"C:\PDFCreator\Employee_Report.pdf"
            };

            var objectSettings = new ObjectSettings
            {
                PagesCount  = true,
                HtmlContent = OrderTemplateGenerator.GetHtmlString(order, false),
                WebSettings = { DefaultEncoding = "utf-8", UserStyleSheet = Path.Combine(Directory.GetCurrentDirectory(), "assets", "invoice.css") },
                // 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 file           = _converter.Convert(pdf);
            var message        = @"
Dear Customer,


Attached is your current invoice from LED Lights and Parts (Pixel Print Ltd). 

If you have a credit account this invoice will be marked Awaiting payment. 

If you already paid this invoice will be marked PAID. and no further action is required. 

If you requested the quote, the invoice will be marked HOLD. 

If you returned or exchanged the invoice will be marked as Return/Exchange or Credit. 

Thank you for working with LED Lights and Parts! We\'re happy to work with you to solve any of your lighting challenges. 

Sincerely,

Shaney

3695 East 1st Ave Vancouver, BC V5M 1C2

Tel: (604) 559-5000

Cel: (778) 839-3352

Fax: (604) 559-5008

www.lightsandparts.com | [email protected]
            ";
            var attachment     = new MemoryStream(file);
            var attachmentName = $"Invoice No {order.OrderId}.pdf";
            var subject        = $"Pixel Print Ltd (LED Lights and Parts) Invoice No {order.OrderId}";

            if (string.IsNullOrEmpty(email))
            {
                email = order.Customer.Email;
            }

            var orderToUpdateEmail = _context.Order.FirstOrDefault(o => o.OrderId == orderId);

            orderToUpdateEmail.Email = email;
            await _context.SaveChangesAsync();

            await _emailSender.SendEmailAsync(email, subject, null, message, attachment, attachmentName, true);

            return(Ok());
        }