rdvojmoc / DinkToPdf

C# .NET Core wrapper for wkhtmltopdf library that uses Webkit engine to convert HTML pages to PDF.
MIT License
1.08k stars 412 forks source link

bootstrap format in html partial view #169

Open mkkb917 opened 1 year ago

mkkb917 commented 1 year ago

Hi I am generating the pdf from razor partial view in asp.net core the pdf is working fine but the style and format are not applied to the pdf I am using bootstrap for styling and formatting the web pages plz consider for my help

`

public async Task<byte[]> CreatePdf(PaperViewVM paperViewVM)
        {
            var view = await _renderer.RenderPartialToStringAsync("_PaperViewBoardLayout", paperViewVM);
            string webRootPath = _webHostEnvironment.WebRootPath;
            string PaperPath = webRootPath + WC.PaperPathPDF;
            string guid = Guid.NewGuid().ToString();
            string fileName = paperViewVM.Setting.SubjectName + guid;
            string UniqueFileName = fileName + ".pdf";   
            var globalSettings = new GlobalSettings
            {
                ColorMode = ColorMode.Color,
                Orientation = Orientation.Portrait,
                PaperSize = PaperKind.A4,
                DPI = 300,
                ImageDPI = 900,
                Margins = new MarginSettings() { Top = 10, Bottom = 10, Left = 10, Right = 10 },
                Out = PaperPath + UniqueFileName,
            };
            var objectSettings = new ObjectSettings
            {
                PagesCount = true,
                HtmlContent = view,             // view is a html string from rendererhtmltostring method
                WebSettings = { DefaultEncoding = "utf-8", UserStyleSheet = Path.Combine(Directory.GetCurrentDirectory(), "assets", "bootstrap.css") }, 
                HeaderSettings = { FontName = "Arial", FontSize = 12, Line = true, Center = "Pdf Paper Generated by ExamSystem.com" },
                FooterSettings = { FontName = "Arial", FontSize = 12, Line = true, Right = "Page [page] of [toPage]" }
            };
            var pdf = new HtmlToPdfDocument()
            {
                GlobalSettings = globalSettings,
                Objects = { objectSettings }
            };
            byte[] file = _pdfConverter.Convert(pdf);         
            var obj = new GeneratedPaper();
            obj.IsSuccess = true;
            obj.PaperPath = WC.PaperPathPDF + UniqueFileName;
            obj.CreatedBy = paperViewVM.Setting.CreatedBy;
            obj.SubjectName = paperViewVM.Setting.SubjectName;
            obj.CreateDate = DateTime.Now.Date;
            return file;
        }`

I tried to use bootstrap file for format reference but its not working am i need to apply html formating styles on view for proper renderring the pdf

eliseumade commented 1 year ago

sorted out? I have the same problem.