DinkToPdf icon indicating copy to clipboard operation
DinkToPdf copied to clipboard

Unable to load DLL 'libwkhtmltox' or one of its dependencies

Open metainilesh opened this issue 5 years ago • 17 comments

Request ID: 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.<Convert>b__0() at System.Threading.Tasks.Task1.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](Func1 delegate) at DinkToPdf.SynchronizedConverter.Convert(IDocument document) at GymCore.Controllers.RegistrationController.CreatePDF(Int32 regno) in C:\Users\UDN002\Desktop\GymCore\GymCore\Controllers\RegistrationController.cs:line 489 at Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments) at System.Threading.Tasks.ValueTask1.get_Result() at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeActionMethodAsync() at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeNextActionFilterAsync() at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync() at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter() at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context) at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync() at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync() at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext) at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.Invoke(HttpContext context) ---> (Inner Exception #0) 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.<Convert>b__0() at System.Threading.Tasks.Task1.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)<---

Still facing the issue can you please help :

I have set the properties of libwkhtmltox.dll image

I tried the both the both debug & release.

I'm stuck please help

metainilesh avatar Mar 05 '19 17:03 metainilesh

What you might be experiencing could be a misleading error message, if you don't have the VSRedist 2015 package installed on the machine/server. (typical for use of c++ libraries)

You could try using my powershell module to detect/silently install the redistributable package: PowershellVsRedistInstaller

You can also just check the powershell code to find where to check in the registry or check where to download the files.

JimWolff avatar Mar 08 '19 20:03 JimWolff

Hello, Check the answer from here.

https://stackoverflow.com/questions/55430532/dinktopdf-net-core-not-able-to-load-dll-files.

I used that and it worked very well.

githithu avatar Mar 30 '19 14:03 githithu

In my case it is working in local but when I'm trying to publish in azure its throwing the following error and failing the whole application An error occurred while starting the application. DllNotFoundException: Unable to load DLL 'D:\home\site\wwwroot\bin\Debug\netcoreapp2.0\libwkhtmltox': The specified module could not be found. (Exception from HRESULT: 0x8007007E) System.Runtime.Loader.AssemblyLoadContext.InternalLoadUnmanagedDllFromPath(string unmanagedDllPath)

DllNotFoundException: Unable to load DLL 'D:\home\site\wwwroot\bin\Debug\netcoreapp2.0\libwkhtmltox': The specified module could not be found. (Exception from HRESULT: 0x8007007E) System.Runtime.Loader.AssemblyLoadContext.InternalLoadUnmanagedDllFromPath(string unmanagedDllPath) System.Runtime.Loader.AssemblyLoadContext.LoadUnmanagedDllFromPath(string unmanagedDllPath) Socrates.ApiServices.Startup+CustomAssemblyLoadContext.LoadUnmanagedDll(string unmanagedDllName) in Startup.cs Socrates.ApiServices.Startup.ConfigureServices(IServiceCollection services) in Startup.cs System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection services) Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices() Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()

BTMutturaj avatar May 16 '19 15:05 BTMutturaj

It's nice work for me:

using System;
using System.Reflection;
using System.Runtime.Loader;
    public class CustomAssemblyLoadContext : AssemblyLoadContext
    {
        public IntPtr LoadUnmanagedLibrary(string absolutePath)
        {
            return LoadUnmanagedDll(absolutePath);
        }
        protected override IntPtr LoadUnmanagedDll(string unmanagedDllName)
        {
            return LoadUnmanagedDllFromPath(unmanagedDllName);
        }
        protected override Assembly Load(AssemblyName assemblyName)
        {
            throw new NotImplementedException();
        }
    }

And before initialize Converter call this one: new CustomAssemblyLoadContext().LoadUnmanagedLibrary($"{pathToYour-libwkhtmltox.dll}/libwkhtmltox.dll");

===>AssemblyLoadContext you can install from nuget System.Runtime.Loader ===>libwkhtmltox.dll you can download here: https://github.com/rdvojmoc/DinkToPdf/tree/master/v0.12.4

olehspidey avatar Aug 07 '19 07:08 olehspidey

download this on server, where you try to use libwkhtmltox.dll https://support.microsoft.com/ru-ru/help/2977003/the-latest-supported-visual-c-downloads

ABakumenko avatar Mar 24 '20 15:03 ABakumenko

Having this issue on an azure function. Has anyone gotten this to work in an azure function? azfunc libs

Domt301 avatar Apr 27 '20 19:04 Domt301

We're running this in a function app, to me it looks like you're pointing to the wrong path. In our case (where the packages are deployed from nuget and part of the build deployed as a package in the function app) it would be something like: D:\home\site\wwwroot\bin the files would be located in. Let me know if this helps.

JimWolff avatar May 21 '20 22:05 JimWolff

It's also worth mentioning that the service plan the app service or function is using cannot be the free tier, runs just fine on S1 (provided all the paths to the executables are correct)

obrocki-fortem avatar May 22 '20 07:05 obrocki-fortem

I think that was my issue. Ended up going another route. Still using an azure function but went with a node package instead. The critical thing to point out is that in the consumption plan you dont have access to GDI support in the sandbox. https://stackoverflow.com/questions/61473525/node-return-byte-array-after-turning-html-to-pdf

Domt301 avatar May 28 '20 19:05 Domt301

download this on server, where you try to use libwkhtmltox.dll https://support.microsoft.com/ru-ru/help/2977003/the-latest-supported-visual-c-downloads

It's worked. Thanks bro

trungttnd avatar Nov 18 '20 04:11 trungttnd

I solved this problem more quickly.

Solution: Add these codes inside the Project.Service.csproj

<ItemGroup>
  <None Remove="libwkhtmltox.dll" />
</ItemGroup>

<ItemGroup>
  <EmbeddedResource Include="libwkhtmltox.dll">
    <CopyToOutputDirectory>Always</CopyToOutputDirectory>
  </EmbeddedResource>
</ItemGroup>

dgokhan avatar Jan 18 '21 08:01 dgokhan

Thanks @dgokhan. Adding the given code to the project's .csproj file worked for me.

Sanushi-Salgado avatar Feb 23 '21 17:02 Sanushi-Salgado

I needed to solve this without installing any packages. I managed to solve it by downloading all 3 files (dll, dylib and so) and putting them into the project directory. Then I added this into the .csproj file: <ItemGroup> <None Update="libwkhtmltox.dll"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory> </None> <None Update="libwkhtmltox.dylib"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory> </None> <None Update="libwkhtmltox.so"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory> </None> </ItemGroup>

sudoman281 avatar Apr 23 '21 10:04 sudoman281

@dgokhan method worked for me. All I had to in addition for it work remotely was placing the libwkhtmltox.dll in the remote server.

h-sanusi avatar May 09 '21 12:05 h-sanusi

I have lost a number of hours trying to resolve this issue on OSX and have finally found the solution which is quite obvious in hindsight. Hopefully it helps someone in the future.

Windows reference - libwkhtmltox.dll OSX reference - libwkhtmltox.dylib Linux reference - libwkhtmltox.os

Obviously make you you reference the correct file for your OS.

https://githubmemory.com/repo/rdvojmoc/DinkToPdf/issues/100

jonyboom avatar Oct 07 '21 10:10 jonyboom

Download package from https://github.com/rdvojmoc/DinkToPdf/tree/master/v0.12.4

Add it to your project reference by custom Assembly reference & register in your startup project. This is custom assembly load context which can load library from absolute path.

internal class CustomAssemblyLoadContext : AssemblyLoadContext
{
	public IntPtr LoadUnmanagedLibrary(string absolutePath)
	{
		return LoadUnmanagedDll(absolutePath);
	}
	protected override IntPtr LoadUnmanagedDll(String unmanagedDllName)
	{
		return LoadUnmanagedDllFromPath(unmanagedDllName);
	}

	protected override Assembly Load(AssemblyName assemblyName)
	{
		throw new NotImplementedException();
	}
}

In Statup.cs add below code. Call CustomAssemblyLoadContext before you create your converter:

CustomAssemblyLoadContext context = new CustomAssemblyLoadContext(); context.LoadUnmanagedLibrary(path);

var converter = new SynchronizedConverter(new PdfTools()); services.AddSingleton(converter); services.AddSingleton(typeof(IConverter), new SynchronizedConverter(new PdfTools()));

SachinDubey123 avatar Dec 27 '21 12:12 SachinDubey123

Hi Team,

I am also facing the same issue with .Net.Core 5.I resolved the issue while doing the below steps.

1.Docker file need to add the below things. RUN apt-get update RUN apt-get install -y wget RUN apt-get install -y curl

RUN cd /app RUN 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

RUN wget https://github.com/rdvojmoc/DinkToPdf/blob/master/v0.12.4/64%20bit/libwkhtmltox.dll RUN wget https://github.com/rdvojmoc/DinkToPdf/blob/master/v0.12.4/64%20bit/libwkhtmltox.dylib RUN wget https://github.com/rdvojmoc/DinkToPdf/blob/master/v0.12.4/64%20bit/libwkhtmltox.so WORKDIR /app COPY --from=publish /app .

  1. Don't add any assembly in solution level.
  2. if you need to local debug # if DEBUG then add dll in project.
  3. This below code in startup.cs file for local debug.

if DEBUG

        CustomAssemblyLoadContext context = new CustomAssemblyLoadContext();
        context.LoadUnmanagedLibrary(Path.Combine(Directory.GetCurrentDirectory(), "libwkhtmltox.dll"));     
        var converter = new SynchronizedConverter(new PdfTools());
        services.AddSingleton(converter);
        services.AddSingleton(typeof(IConverter), new SynchronizedConverter(new PdfTools()));

#Endif Thanks, Manish

ManishAzure avatar Jul 21 '22 18:07 ManishAzure

If this is important for someone, I add the code like in the previous answers, additionally when I tried to publish my app there was the error 500.30 because the architecture in the azure portarl, so change the platform for 64bits or 32bits and thats all.

Engel02 avatar Jul 21 '23 00:07 Engel02