rdvojmoc / DinkToPdf

C# .NET Core wrapper for wkhtmltopdf library that uses Webkit engine to convert HTML pages to PDF.
MIT License
1.09k stars 417 forks source link

Unable to load shared library 'libwkhtmltox' or one of its dependencies #116

Closed Faizal-Majeed closed 4 years ago

Faizal-Majeed commented 4 years ago

I'm having an issue with DInkToPDF on alpine docker, when i run my app i'm getting the following error.

Unhandled exception. System.DllNotFoundException: Unable to load shared library 'libwkhtmltox' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: Error loading shared library liblibwkhtmltox: No such file or directory at DinkToPdf.WkHtmlToXBindings.wkhtmltopdf_init(Int32 useGraphics) at DinkToPdf.PdfTools.Load() at DinkToPdf.BasicConverter.Convert(IDocument document) at TestPDF.Program.Main(String[] args) in /source/TestPDF/Program.cs:line 53

I have tried different solution that are posted by other peoples in this repo, nothing worked, So I have attached my docker file as well, please review and help me to solve this issue

Docker File

FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
WORKDIR /source

COPY ["TestPDF/TestPDF.csproj", "TestPDF/"]
COPY ["TestPDF/libwkhtmltox.dll", "TestPDF/"]
COPY ["TestPDF/libwkhtmltox.dylib", "TestPDF/"]
COPY ["TestPDF/libwkhtmltox.so", "TestPDF/"]
RUN dotnet restore "TestPDF/TestPDF.csproj" -r linux-musl-x64

COPY . .
WORKDIR "/source/TestPDF"
RUN dotnet build "TestPDF.csproj" -c Release -o /app -r linux-musl-x64

FROM build AS publish
RUN dotnet publish "TestPDF.csproj" -c Release -o /app -r linux-musl-x64 --self-contained false --no-restore

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-alpine
WORKDIR /app
COPY --from=build /app ./
RUN mkdir documentengine
ENTRYPOINT ["dotnet", "TestPDF.dll"]

My application is a simple console app the render a pdf file, And I'm using 64bit native libraries.

bronzewind commented 4 years ago

Not sure if you have a typo, I'm also having issues in .Net Core 3.1 but I do find the dll. Please check the rror you have copied and pasted refers to a library named liblibwkhtmltox but the the library name is libwkhtmltox. This url https://code-maze.com/create-pdf-dotnetcore/ has all the steps but unfortunately I'm having an error while loading the dll, both local and deployed.

Faizal-Majeed commented 4 years ago

@bronzewind - Thank for the reply. I noticed that as well, I checked i didn't misspelled anywhere. Not sure why i'm getting this name liblibwkhtmltox. Also I tried to load the dll using code like,

            var dlls = Path.Combine(AppContext.BaseDirectory, "libwkhtmltox.dll");
            var context = new CustomAssemblyLoadContext();            
            context.LoadUnmanagedLibrary(dlls);

its throwing an error saying cant file the dll. See the attached image

dinktopdf-docker

the dll is in root folder only, don't know why it can find the file. However it works in my local .

bronzewind commented 4 years ago

You are basically having the same issue I did. I think this wrapper must be reviewed. What I did is I'm using the base library https://wkhtmltopdf.org/ which does not have so many features. You can mix https://blog.elmah.io/generate-a-pdf-from-asp-net-core-for-free/ and https://code-maze.com/create-pdf-dotnetcore/ to make it work.

Faizal-Majeed commented 4 years ago

Do you have any sample work that i can refer ? mainly that works in linux docker container

bronzewind commented 4 years ago

I do not. Just follow the first link in my last sentence of my previous post and it should work.

Faizal-Majeed commented 4 years ago

FYI, I made it work with windows container here is my docker file.

#Depending on the operating system of the host machines(s) that will build or run the containers, the image specified in the FROM statement may need to be changed.
#For more information, please see https://aka.ms/containercompat

FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS base
WORKDIR /app

FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
WORKDIR /src
COPY ["TestPDF/TestPDF.csproj", "TestPDF/"]
RUN dotnet restore "TestPDF/TestPDF.csproj"
COPY . .
WORKDIR "/src/TestPDF"
RUN dotnet build "TestPDF.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "TestPDF.csproj" -c Release -o /app/publish

FROM base AS final
RUN apt-get update \
    && apt-get install libgdiplus -y \
    && rm -rf /var/lib/apt/lists/*
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "TestPDF.dll"]

Trying to make it work on Linux container. Used buster-slim linux instead of alpine . It worked.

FROM mcr.microsoft.com/dotnet/core/runtime:3.1-buster-slim AS base
WORKDIR /app

FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build

WORKDIR /src
COPY ["TestPDF/TestPDF.csproj", "TestPDF/"]
RUN dotnet restore "TestPDF/TestPDF.csproj"
COPY . .
WORKDIR "/src/TestPDF"
RUN dotnet build "TestPDF.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "TestPDF.csproj" -c Release -o /app/publish

FROM base AS final
RUN apt-get update \
    && apt-get install libgdiplus -y \
    && rm -rf /var/lib/apt/lists/*
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "TestPDF.dll"]

@bronzewind - Thanks, the link you shared was useful. So I decided to go with DinkToPdf library and buster-slim base image. I posted my windows and linux container docker file, hope that helps you as well.

reveation-labs commented 4 years ago

@Faizal-Majeed

I tried with exact same dockerfile that you shared for buster-slim base image. But when I run the app in Azure App Service for Linux, I get below exception:

2020-08-11T05:31:27.255347717Z Unhandled exception. System.DllNotFoundException: Unable to load shared library '/app/libwkhtmltox.dll' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: /app/libwkhtmltox.dll: cannot open shared object file: No such file or directory
2020-08-11T05:31:27.255404119Z    at System.Runtime.Loader.AssemblyLoadContext.InternalLoadUnmanagedDllFromPath(String unmanagedDllPath)
2020-08-11T05:31:27.255415520Z    at System.Runtime.Loader.AssemblyLoadContext.LoadUnmanagedDllFromPath(String unmanagedDllPath)

Did you run into the same issue? I am wondering if I need to add more steps.

pajarnas commented 2 years ago

@Faizal-Majeed

I tried with exact same dockerfile that you shared for buster-slim base image. But when I run the app in Azure App Service for Linux, I get below exception:

2020-08-11T05:31:27.255347717Z Unhandled exception. System.DllNotFoundException: Unable to load shared library '/app/libwkhtmltox.dll' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: /app/libwkhtmltox.dll: cannot open shared object file: No such file or directory
2020-08-11T05:31:27.255404119Z    at System.Runtime.Loader.AssemblyLoadContext.InternalLoadUnmanagedDllFromPath(String unmanagedDllPath)
2020-08-11T05:31:27.255415520Z    at System.Runtime.Loader.AssemblyLoadContext.LoadUnmanagedDllFromPath(String unmanagedDllPath)

Did you run into the same issue? I am wondering if I need to add more steps.

Same here How did you fix this?