.Net Core PDF Library from HTML (DinkToPdf)

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.


Comments

6 responses to “.Net Core PDF Library from HTML (DinkToPdf)”

  1. I used DinkToPdfIncludesDependencies on windows still I’m getting this error.

    An unhandled exception has occurred while executing the request.
    System.AggregateException: One or more errors occurred. (Unable to load DLL ‘libwkhtmltox’ or one of its dependencies: The specified module could not be found. (Exception from HRESULT: 0x8007007E)) —> System.DllNotFoundException: Unable to load DLL ‘libwkhtmltox’ or one of its dependencies: The specified module could not be found. (Exception from HRESULT: 0x8007007E)
    at DinkToPdf.WkHtmlToXBindings.wkhtmltopdf_init(Int32 useGraphics)
    at DinkToPdf.PdfTools.Load()
    at DinkToPdf.BasicConverter.Convert(IDocument document)
    at DinkToPdf.SynchronizedConverter.n__0(IDocument document)
    at DinkToPdf.SynchronizedConverter.c__DisplayClass5_0.b__0()
    at System.Threading.Tasks.Task`1.InnerInvoke()
    at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
    — End of stack trace from previous location where exception was thrown —
    at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot)
    — End of inner exception stack trace —
    at DinkToPdf.SynchronizedConverter.Invoke[TResult](Func`1 delegate)
    at DinkToPdf.SynchronizedConverter.Convert(IDocument document)
    at PDF_Generator.Controllers.PdfCreatorController.CreatePDF(CreatePdfCommand diagnosticsPdfDownloadInfo) in C:\Users\amshete\Desktop\download task\create-pdf-with-netcore-master\create-pdf-with-netcore-master\PDF_Generator\PDF_Generator\Controllers\PdfCreatorController.cs:line 68
    at lambda_method(Closure , Object , Object[] )
    at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters)
    at Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor.SyncActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)

  2. Hi Amol,

    try to install “MS visual C++ 2015 redistributable package” this should solve the issue. If you need more information look at this thread: https://github.com/rdvojmoc/DinkToPdf/issues/5

    I hope it works.

  3. I have the same above problem in Windows server 2012 R2, tried all the ways and spend more than a week no luck, could you please help me to resolve this issue

  4. I am also getting this issue what to do?
    Unable to load DLL ‘libwkhtmltox’ or one of its dependencies: The specified module could not be found.

  5. you need to copy the libwkhtmltox.dll to the root folder of your project

  6. Where can i find this dll libwkhtmltox.dll

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.