コード例 #1
0
        public IActionResult CreatePDF(Guid agendamentoId)
        {
            try
            {
                var contratoPdf = _contratoService.ObterContrato(agendamentoId);

                var globalSettings = new GlobalSettings
                {
                    ColorMode   = ColorMode.Color,
                    Orientation = Orientation.Portrait,
                    PaperSize   = PaperKind.A4,
                    Margins     = new MarginSettings {
                        Top = 10
                    },
                    DocumentTitle = "PDF Report"
                };
                var objectSettings = new ObjectSettings
                {
                    PagesCount     = true,
                    HtmlContent    = TemplateGenerator.ObterTemplateContrato(contratoPdf),
                    WebSettings    = { DefaultEncoding = "utf-8", UserStyleSheet = Path.Combine(Directory.GetCurrentDirectory(), "Assets", "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", "contrato.pdf"));
            }
            catch {
                return(Ok(new ErrorModel("Erro inesperado ao gerar o contrato!")));
            }
        }