Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dockerized Dotnet Core 2.1 throws Gdip exception when using Select.HtmlToPdf.NetCore

I am using Select.HtmlToPdf.NetCore(18.3.0) to convert Html to pdf in Dotnetcore 2.1. It is perfectly working in local environment but when hosted with Docker it throws an error saying,

{"fileName":"System.TypeInitializationException: The type initializer for 'Gdip' threw an exception. ---> System.DllNotFoundException: Unable to load shared library 'libdl' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: liblibdl: cannot open shared object file: No such file or directory\n at Interop.Libdl.dlopen(String fileName, Int32 flag)\n at System.Drawing.SafeNativeMethods.Gdip.LoadNativeLibrary()\n at System.Drawing.SafeNativeMethods.Gdip..cctor()\n --- End of inner exception stack trace ---\n at System.Drawing.SafeNativeMethods.Gdip.GdipNewPrivateFontCollection(IntPtr& fontCollection)\n at SelectPdf.Lib.ᡜ..ctor()\n at SelectPdf.Lib.៞..ctor()\n at SelectPdf.Lib.៞..ctor(ᡏ A_0, ᠝ A_1)\n
at SelectPdf.HtmlToPdf.ᜁ(String A_0, String A_1, String A_2, String A_3, Boolean A_4)\n at SelectPdf.HtmlToPdf.ConvertHtmlString(String htmlString)

I have tried by adding these set of lines in Dockerfile, but still having the same error.

FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base  
RUN apt-get update  
RUN apt-get install -y apt-utils  
RUN apt-get install -y libgdiplus  
RUN ln -s /usr/lib/libgdiplus.so /usr/lib/gdiplus.dll  
WORKDIR /app  
EXPOSE 80  
like image 743
Isuru Amarathunga Avatar asked Aug 29 '18 05:08

Isuru Amarathunga


4 Answers

I know It is an old question but I encountered this problem lately and I would like to share with my solution.
Select.HtmlToPdf.NetCore do not work on linux
Just do not. If you already have generated string with html I would advise you to find this nuget: Haukcode.DinkToPdf
To find some tutorials just google DinkToPdf. Haukcode is just updated and more docker friendly version.
some dockerfile stuff:
FROM microsoft/aspnetcore:2.0.0 as Base RUN apt-get update RUN apt-get install -y apt-utils RUN apt-get install -y libgdiplus RUN apt-get install -y libc6-dev RUN ln -s /usr/lib/libgdiplus.so/usr/lib/gdiplus.dll ... I hope it will help someone :)

like image 188
Arkadiusz Raszeja Avatar answered Oct 14 '22 18:10

Arkadiusz Raszeja


this worked for me. I'm running a Debian server, using System.Drawing.Common 4.7.0

RUN apt-get update \
&& apt-get install -y --allow-unauthenticated \
    libc6-dev \
    libgdiplus \
    libx11-dev \
 && rm -rf /var/lib/apt/lists/*
like image 28
ali zarei Avatar answered Oct 14 '22 17:10

ali zarei


You need to install libc6-dev in your Docker container. You can install libc6-dev by running the following command:

RUN apt-get install -y libc6-dev

Let me know how that goes.

like image 2
Frederik Carlier Avatar answered Oct 14 '22 18:10

Frederik Carlier


I was facing the same problem too using iTextSharp 5.5.13.1 and the way I've solved is combine @Frederik Carlier's solution in my Dockerfile:

RUN apt-get update && apt-get -y install libxml2 libgdiplus libc6-dev

After that, i've restarted Rancher Container and Rancher User Stack and it works fine.

(Runtime used: FROM microsoft/dotnet:2.2-aspnetcore-runtime)

like image 1
Ilich Morales Avatar answered Oct 14 '22 17:10

Ilich Morales