Esempio n. 1
0
        public async Task <IActionResult> CreateRecipePDFAsync(int consultationId)
        {
            try
            {
                var fullRecipeInfoDto = await _recipeRepository.GetRecipe(consultationId);

                var globalSettings = new GlobalSettings
                {
                    ColorMode   = ColorMode.Color,
                    Orientation = Orientation.Portrait,
                    PaperSize   = PaperKind.A4,
                    Margins     = new MarginSettings {
                        Top = 10
                    },
                    DocumentTitle = "Reteta"
                };
                var styleSheet     = _context.EmailTemplates.FirstOrDefault(c => c.Id == 5).Template;
                var objectSettings = new ObjectSettings
                {
                    PagesCount  = true,
                    HtmlContent = TemplateGenerator.GetRecipeHTMLString(fullRecipeInfoDto),
                    /* WebSettings = { DefaultEncoding = "utf-8", UserStyleSheet = styleSheet }, */
                    WebSettings    = { DefaultEncoding = "utf-8", UserStyleSheet = Path.Combine(Directory.GetCurrentDirectory(), "wassets", "styles.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);
                return(File(file, "application/pdf", "Reteta"));
            }
            catch (Exception ex)
            {
                var error = new Error
                {
                    Message      = ex.Message,
                    InnerMessage = ex.InnerException.Message,
                    StackTrace   = ex.InnerException.StackTrace,
                };
                _context.Errors.Add(error);
                await _context.SaveChangesAsync();

                return(BadRequest("Upps..ceva nu a mers!"));
            }
        }