8

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)

4
  • Is it just okay if you import dinktopdf using nuget? insead of loading it directly as unamange?
    – keysl
    Commented Mar 30, 2019 at 10:43
  • Hello, I have imported. I have installed it well. In its description of use, I must import the dll files from the run. But this method Directory.GetCurrentDirectory() seems to not bet working on Asp.Net Core 3.0 Commented Mar 30, 2019 at 10:53
  • Asp.Net Core Directory.GetCurrentDirectory() I think is the culprit here. It is returning C:\Program Files\IIS Express\ as the default folder for the project Commented Mar 30, 2019 at 10:57
  • Farzaneh Talebi posted an Answer saying "I had same problem and this tutorial helped me: medium.com/volosoft/…"
    – Scratte
    Commented Jan 18, 2021 at 14:03

5 Answers 5

14

Just in case anyone else is having the same issue I was able to solve it by installing Microsoft Visual C++ 2015 Redistributable.

4
  • 1
    I had a similar issue when loading a legacy 32bit dll in a UAT environment. The C++ 2010 Redistributable worked for this particular dll.
    – nf313743
    Commented Sep 11, 2020 at 15:48
  • Thx Very Much...local was ok but production - other serwer not working - YOU SAVE MY DAY Commented Sep 2, 2021 at 13:42
  • Wow, installing Microsoft Visual C++ 2015 Redistributable works for me, here is the link to this microsoft.com/en-us/download/details.aspx?id=52685 Commented Apr 14, 2023 at 7:53
  • @LuqmanCheema We're sorry, this download is no longer available. - now what?
    – Cloud
    Commented Aug 10, 2023 at 12:36
10

It is mentioned in library's git repo that you should download binaries and include them in your source code:

Copy native library to root folder of your project. From there .NET Core loads native library when native method is called with P/Invoke. You can find latest version of native library here. Select appropriate library for your OS and platform (64 or 32 bit).

What was breaking things was that I was going to that url and right click on each file and select save link as(chrome). This leads to a broken file being downloaded: enter image description here

DON'T DO THAT you have to open each file within github and then use that Download button. The healthy file is much bigger than what you would get if you go the wrong way!

enter image description here

ridiculous but the problem may be caused by this ...

1
  • 1
    I am using this solution, and it's now worked for me. So I just manually download the dll file, even I have libwkhtmltox.dylib and libwkhtmltox.so files. Commented May 30, 2022 at 4:35
2

On Asp.Net Core application I use it like this to get the current directory on runtime

#if DEBUG
            //windows
            string filePath = $@"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}\libwkhtmltox.dll";
#else
            //linux
            string filePath = @$"{(($@"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}/libwkhtmltox.so").Replace(@"\", @"/"))}";
#endif
            CustomAssemblyLoadContext context = new CustomAssemblyLoadContext();
            context.LoadUnmanagedLibrary(filePath);
            serviceCollection.AddSingleton(typeof(IConverter), new SynchronizedConverter(new PdfTools()));
#endregion
2
  • 2
    I would recommend using System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform() instead, check the platform if it's Windows or Linux and set the appropriate path. This way Debug won't be tied to Linux. Commented Aug 26, 2020 at 10:26
  • Also recommending this approach stackoverflow.com/a/38795621/4745982. Works nice for all platforms Commented Aug 7, 2022 at 16:53
2

if you are using asp.net core on Linux then you need to install required packages by using following command

apt-get update \
&& apt-get install -y --no-install-recommends \
    zlib1g \
    fontconfig \
    libfreetype6 \
    libx11-6 \
    libxext6 \
    libxrender1 \
&& curl -o /usr/lib/libwkhtmltox.so \
    --location \
    https://github.com/rdvojmoc/DinkToPdf/raw/v1.0.8/v0.12.4/64%20bit/libwkhtmltox.so
2
  • this command need to be used by root permissions, we need to call 'sudo su' command before using this
    – Zeshan
    Commented Jan 18, 2022 at 12:25
  • following commands helped when DinkToPdf was not showing images properly sudo apt-get install libgdiplus sudo apt-get install xvfb libfontconfig wkhtmltopdf sudo apt-get install libc6-dev sudo apt-get install openssl sudo apt-get install libssl1.0-dev
    – Zeshan
    Commented Jan 18, 2022 at 16:03
1

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");
2
  • 2
    It does work locally,however when I publish to azure an error occured.
    – Swaroop
    Commented Jun 10, 2019 at 9:57
  • 1
    What of mac . ? Commented Jul 18, 2019 at 16:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.