If you need an PDF library, wich converts an HTML document to a PDF you need a library. There are a lot of libraries on the market, but only a few wich are free and open source. In my projects i used the DinkToPdf library, wich is a wrapper for the webkit engine (the chrome engine) for c#/.Net Core. The library needs as dependency the wkhtmltopdf library, wich you need to add manually.
If you doesn’t want do the extra work and want a package wich will do everything for you, i have found the library DinkToPdfIncludesDependencies, wich includes the DinkToPdf library and the wkhtmltopdf library, so you only need to add this nuget package and you are fine. I had only on linux os problems with this library and must add the wkhtmltopdf library for linux manually, but on windows this library should work fine.
I have used the library to generate quickly a pdf from an HTML webpage. Here is an example from an .net core webapplication. Method in a controller:
/// <summary> /// Get report pdf. /// </summary> /// <param name="configId"/>Config guid. /// <returns>Content.</returns> [HttpGet("Pdf")] [HttpGet("Pdf/{configId}")] public IActionResult Pdf(Guid configId) { string sercret = this.configuration["PdfSettings:PdfSecret"]; var doc = new HtmlToPdfDocument() { GlobalSettings = { ColorMode = ColorMode.Color, Orientation = Orientation.Portrait, PaperSize = PaperKind.A4, DocumentTitle = "PDF Report", Margins = new MarginSettings() { Top = 10 }, }, Objects = { new ObjectSettings() { Page = $"{this.Request.Scheme}://{this.Request.Host}/timeline/Reporting/Show/" + sercret + "/" + configId.ToString(), }, }, }; byte[] pdf = this.converter.Convert(doc); return this.File(pdf, "application/pdf", DateTime.Now.ToLocalTime() + " Report.pdf"); }
I hope you enjoy the library.
Leave a Reply