When creating pdf of html page with dinktopdf library, correct display does not occur

417 Views Asked by At

I want to render the html page and create a pdf file with the dinktopdf library. The pdf file I created does not have the same view as my view page. it doesn't get the table structure, it doesn't see the text-center lines and starts the text from the left and doesn't take the png images either, they appear blank. My class that I created pdf is as below. Here I make my settings about the dinktopdf library and convert the html page to pdf.

public async Task<byte[]> GeneratePDF2(string htmlContent)
        {
            var doc = new HtmlToPdfDocument()
            {
                GlobalSettings =
                {
                    ColorMode = ColorMode.Color,
                    Orientation = Orientation.Portrait,
                    PaperSize = PaperKind.A4,
                },
                Objects = 
                {
                    new ObjectSettings()
                    {
                        PagesCount = true,
                        HtmlContent = htmlContent,
                        WebSettings = { DefaultEncoding = "utf-8" },
                        FooterSettings = { FontName = "Arial", FontSize = 9, Right = "Sayfa [page] / [toPage]", Line = true }
                    }
                }
            };

            var pdf = _converter.Convert(doc);
            return pdf;
        }

here is the PDFController. When I say create pdf on my view page, it connects to the relevant controller and fills my model.

public async Task<IActionResult> GeneratePDF(Guid GUID)
        {
            Certificate_Dto model = new Certificate_Dto();

            var applicationVW = await _serviceManager.VW_Applications.GetEntityAsync(x => x.GUID == GUID);
            if (applicationVW != null)
            {
                string CourseMakersJSON = _serviceManager.Courses.GetValue(a => a.CourseID == applicationVW.CourseID, "CourseMakersJSON");

                List<int> CourseTypeMakerIDs = new();

                if (!string.IsNullOrEmpty(CourseMakersJSON))
                {
                    CourseTypeMakerIDs = CourseMakersJSON.Split(',').Select(int.Parse).ToList();
                }

                model = new()
                {
                    //process
                };
            }

            var htmlContent = await RenderViewToStringAsync(HttpContext, "Views/Application/P_Certificate.cshtml", model);
            var pdfData = await _pdfService.GeneratePDF2(htmlContent);
            var fileName = $"{DateTime.Now:yyyyMMddHHmmss}.pdf";
            Response.Headers.Add("Content-Disposition", $"inline; filename={fileName}");
            return File(pdfData, "application/pdf");


        }


        private async Task<string> RenderViewToStringAsync<TModel>(HttpContext httpContext, string viewName, TModel model)
        {
            var routeData = new RouteData();
            routeData.Values["controller"] = "Application";
            var actionContext = new ActionContext(httpContext, routeData, new ActionDescriptor());

            var viewResult = _viewEngine.GetView(executingFilePath: null, viewPath: viewName, isMainPage: true);
            if (!viewResult.Success)
            {
                throw new InvalidOperationException($"Sayfa bulunamadı: {viewName}");
            }

            var viewDictionary = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary())
            {
                Model = model
            };

            using var sw = new StringWriter();
            var viewContext = new ViewContext(
                actionContext,
                viewResult.View,
                viewDictionary,
                new TempDataDictionary(actionContext.HttpContext, _tempDataProvider),
                sw,
                new HtmlHelperOptions());

            await viewResult.View.RenderAsync(viewContext);

            return sw.ToString();
        }

When I want to get a picture, the code on my view page is as follows

<img src="~/Images/Image/logo-1.png" alt="Logo" style="height:70px" srcset="" />

The code of the places where the text-center should be is on the view page like this;

<div class="col col-12 text-center" >

what can i do about it? My view page and pdf view do not match, can you help with this?

0

There are 0 best solutions below