Search code examples
c#.net-corewkhtmltopdfhtml-to-pdfdinktopdf

DinkToPdf Net Core not able to load DLL files


I am trying to generate PDFs from HTML SQL server database using DinkToPdf library.

In the startup file I have added:

var context = new CustomAssemblyLoadContext();
context.LoadUnmanagedLibrary(Path.Combine(Directory.GetCurrentDirectory(), "libwkhtmltox.dll"));

The line gives me this error on launching the web app:

DllNotFoundException: Unable to load DLL 'C:\Program Files\IIS Express\libwkhtmltox.dll' or one of its dependencies: The specified module could not be found. (Exception from HRESULT: 0x8007007E)

System.Runtime.Loader.AssemblyLoadContext.InternalLoadUnmanagedDllFromPath(string unmanagedDllPath)

DllNotFoundException: Unable to load DLL 'C:\Program Files\IIS Express\libwkhtmltox.dll' or one of its dependencies: The specified module could not be found. (Exception from HRESULT: 0x8007007E)


Solution

  • I found some work-arounds. They are not perfect but worth a try, and they did do help and I was able to generate PDFs from SQl Server. I put the .dll files in the following folder and it worked.

    C:\Program Files\IIS Express

    and the loaded the .dll files with

    Path.Combine(Directory.GetCurrentDirectory(), "libwkhtmltox.dll");
    

    The other way I went for the whole Path

    context.LoadUnmanagedLibrary(Path.GetFullPath(@"C:\Users\User\source\repos\WebSolution\WebApp\libwkhtmltox.dll"));
    

    Both of them worked. However, I urge Net Core developers to work on the GetCurrentDir very well. Or a Method to load from the Project or Solution Folder

    Path.Combine(Directory.GetCurrentDirectory(), "libwkhtmltox.dll");