How can I run DinkToPdf on Azure Function ( Linux )

264 views Asked by At

We need to generate PDF using Azure Function on Linux. I tried a lot of options but was unable to run docker with correct configuration.

Here is my docker file:

FROM mcr.microsoft.com/azure-functions/dotnet:4 AS base
WORKDIR /home/site/wwwroot
RUN apt-get update && apt-get install -y libglib2.0 libgdiplus libnss3 libatk1.0-0 libatk-bridge2.0-0 ca-certificates fonts-liberation libappindicator3-1 libasound2 libatk-bridge2.0-0 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgbm1 libgcc1 libglib2.0-0 libgtk-3-0 libnspr4 libnss3 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 lsb-release xdg-utils
EXPOSE 80

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

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

FROM base AS final
WORKDIR /home/site/wwwroot
COPY --from=publish /app/publish .

# Copy files from /site/wwwroot to /site/wwwroot/bin/Debug/net6.0
COPY --from=base /home/site/wwwroot /home/site/wwwroot/bin/Debug/net6.0

ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
    AzureFunctionsJobHost__Logging__Console__IsEnabled=true

I always get One or more errors occurred. (Unable to load shared library '/home/site/wwwroot/bin/Debug/net6.0/bin/libwkhtmltox.so' or one of its dependencies.

I tried to configure docker file to add missing dependencies but it didn't work, still have the same error.

1

There are 1 answers

0
Suresh Chikkam On

One or more errors occurred. (Unable to load shared library '/home/site/wwwroot/bin/Debug/net6.0/bin/libwkhtmltox.so' or one of its dependencies.

  • You need to identify which package contains the 'libwkhtmltox.so' library. use the "apt-file" utility to search for the library in the Ubuntu package repositories. Install "apt-file"
  • update its database and missing library check the below commands.
RUN apt-get update && apt-get install -y apt-file
RUN apt-file update

RUN apt-file search libwkhtmltox.so //missing library

I created a sample function app which generates html file to pdf. here is my Dockerfile templates that which I used.

# Use the base image for Azure Functions on Linux
FROM mcr.microsoft.com/azure-functions/dotnet:4.0 AS build
WORKDIR /home/site/wwwroot

# Copy the Function App code to the container
COPY . .

# Install additional dependencies for PDF generation with iTextSharp
RUN apt-get update && apt-get install -y \
    libgdiplus \
    libxrender1 \
    libfontconfig1

# Set the entry point for the Azure Functions host
CMD [ "dotnet", "Microsoft.Azure.WebJobs.Script.WebHost.dll" ]
  • With this Dockerfile, we don't need to restore NuGet packages, Azure Functions will handle that for us when it runs the function.

Firstly I convert my function to image and next to docker container.

enter image description here

Containerized: enter image description here

  • Then, created Azure container registry (ACR) in portal for the function app deployment. Function app runtime is Linux as per the requirement.

Push your Image to ACR by following command: docker push ACR_name.azurecr.io/functionapp10:latest1

enter image description here

Configure your function app to deploy ACR image into the function application.

enter image description here

Deployment status: enter image description here

Output: enter image description here