Search code examples
pythonazuredockerwkhtmltopdfpdfkit

How to install pdfkit/wkhtmltopdf in Python Azure Function Docker Container?


I'm using the library pdfkit to render a PDF from an HTML template. The library is just a wrapper for wkhtmltopdf and that is a pain in the butt to install.

The generic Dockerfile given by the python Azure Function is this

FROM mcr.microsoft.com/azure-functions/python:3.0-python3.8

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

COPY requirements.txt /
RUN pip install -r /requirements.txt

COPY . /home/site/wwwroot

I'm pretty new to Docker, but I understand that the pdfkit library can just be added to my requirements. I just don't know how to deal with wkhtmltopdf.

I saw this solution, but how would I work this solution into my Dockerfile?

# Create image based on the official openjdk 8-jre-alpine image from the dockerhub
FROM openjdk:8-jre-alpine

# Install wkhtmltopdf
RUN apk add --no-cache wkhtmltopdf

ENTRYPOINT ["wkhtmltopdf"]

Solution

    • If you want to install the wkhtmltopdf in the docker you can do it using a package manager of the image you are using.

    • Assuming you are using a Debian based image you can run the following command:

    sudo  apt-get  install  wkhtmltopdf
    
    • Now your Dockerfile Should look like this
    FROM mcr.microsoft.com/azure-functions/python:4-python3.10
    
    RUN  sudo  apt-get  install  wkhtmltopdf
    
    ENV  AzureWebJobsScriptRoot=/home/site/wwwroot  \
    
    AzureFunctionsJobHost__Logging__Console__IsEnabled=true
    
    COPY  requirements.txt  /
    
    RUN  pip  install  -r  /requirements.txt
    
    COPY  .  /home/site/wwwroot
    
    • Except the installation of wkhtmltopdf the above DockerFile is autogenerated using the command
    func init . --docker
    

    Refer this MSDOC on deploying azure function using docker