4

I am trying to deploy a flask (python) app that uses wkhtmltopdf.

Everything works perfectly when it is run in a debug environment, however when I run it using docker, it stops showing footers and headers.

I suspect it has something to do with the way Docker installs wkhtmltopdf.

Here is my Dockerfile

# set base image (host OS)
FROM python:3.9
RUN apt-get update && \
    apt-get install -y locales && \
    sed -i -e 's/# es_ES.UTF-8 UTF-8/es_ES.UTF-8 UTF-8/' /etc/locale.gen && \
    dpkg-reconfigure --frontend=noninteractive locales
ENV LC_TIME es_ES.UTF-8

# I BELIEVE I AM MISSING SOMETHING HERE
RUN apt-get install wkhtmltopdf -y

# set the working directory in the container
WORKDIR /app
ENV PYTHONPATH "${PYTHONPATH}:/app/src"

# copy the dependencies file to the working directory
COPY requirements.txt .

# install dependencies
RUN pip install -r requirements.txt

# copy the content of the local src directory to the working directory
COPY . .

# command to run on container start
CMD [ "gunicorn", "src.wsgi:app","--bind","0.0.0.0:5000" ]

Here are the relevante python requirements in case they might be useful.

PyPDF2==1.26.0
pdfkit==0.6.1

Any help will be much appreciated. Thanks!

  • 1
    Facing the same issue. Neither footer nor header shows in the PDF output. Have you found a solution? – 100grams Jan 29 '22 at 12:39
  • 1
    Are you getting the same version of wkhtmltopdf on the host and the docker images? (Probably best to pin these dependencies by installing the binary of the specific version from it's GitHub release) – gavin Aug 24 '23 at 06:27
  • 2
    The OS you are running in the docker image probably doesn't have the fonts or ability to render the HTML the same as your local environment. See related issue and solution: [Different output on Docker #4639](https://github.com/wkhtmltopdf/wkhtmltopdf/issues/4639#issuecomment-630146910) – user7434398 Aug 24 '23 at 23:52

1 Answers1

0

@user7434398's comment on your question is most likely the correct answer.

It looks like the headers and footers deal with fonts starting here, and are delegating rendering to QFont. If QFont doesn't recognize the name of the font being requested, it tries to guess what you want, with what are likely to be unpredictable results (in my experience, anyway.) While your typical browser would probably use a standard font for headers and footers, wkhtmltopdf is meant to be used in a headless environment, and it's hard to say what it would do in a docker OS that's different from your debug environment.

The FROM python:3.9 points to bookworm, which is based on debian. If your debug environment isn't debian-based, that's probably why there's a difference.

You might try basing your docker image on the same OS as your debug environment, and installing python in it. Alternatively, you could follow the script that is pointed to by the content in the link in @user7434398's comment, which is here, but it's seriously outdated, and I'd be looking for something more recent if I were you.

Bill Horvath
  • 1,336
  • 9
  • 24