rdvojmoc / DinkToPdf

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

Convert a page .cshtml file to PDF using DinkToPdf #137

Open kaash89 opened 3 years ago

kaash89 commented 3 years ago

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, Page = "PrintTestPage", //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 = _convertor.Convert(pdf); return File(file, "application/pdf");

Please Guide , getting unable to local document , The Page "PrintTestPage" is located in the Pages Directory of my .NET CORE Application

Markuzy commented 3 years ago

It has been a few months since your post and I think you may have found the solution elsewhere, but here's mine for record keeping.


Your cshtml file cannot be rendered because it isn't a html page, a cshtml may contain razor code and other c# dependencies that needs razor engine to parse first. If your page is a static page, consider not using cshtml, else you can follow what we did below.

Step one is to create your common function to convert cshtml to the final html string via something like this stackoverflow answer.

Then instead of Page = "PrintTestPage", you should use HtmlContent = myHtmlStr,

The variable myHtmlStr is a string generated by step one. This should be the basis of where you should begin.

I hope this helps.