Skip to main content

Adding wkhtmltopdf to the Sylius Alpine Docker image

This blog post might be outdated!
This blog post was published more than one year ago and might be outdated!
· 2 min read
Stephan Hochdörfer
Head of IT Business Operations

While preparing a Sylius Docker image for one of our merchants, I realized that I needed to add wkhtmltopdf to the Alpine Docker image as it is a requirement of the Sylius Invoicing Plugin we are using in the project.

Not a big deal, I thought. But when I tried running apk add --no-cache wkhtmltopdf in Alpine to install the wkhtmltopdf package, I got the following error message:

ERROR: unable to select packages:
wkhtmltopdf (no such package):
required by: world[wkhtmltopdf]

At first, I thought maybe the package was called differently on Alpine, but a quick search on pkgs.alpinelinux.org showed that Alpine 3.14 was the last version with which the wkhtmltopdf package was shipped.

While checking for alternative solutions, I came across the Surnet/docker-wkhtmltopdf GitHub repository, which offers wkhtmltopdf builds for multiple Alpine images, e.g., Alpine 3.19, which is the version I wanted to use initially.

That means, I can include the Docker image in my multi-stage Docker build and copy the needed files over. A very stripped-down version of my Dockerfile looks like this:

FROM surnet/alpine-wkhtmltopdf:3.16.2-0.12.6-full as wkhtmltopdf

FROM php:8.2-fpm-alpine3.19 AS sylius_php

# install wkhtmltopdf runtime dependencies
RUN apk add --no-cache \
libstdc++ \
libx11 \
libxrender \
libxext \
libssl3 \
ca-certificates \
fontconfig \
freetype \
ttf-droid \
ttf-freefont \
ttf-liberation;

# Copy needed wkhtmltopdf files from wkhtmltopdf image
COPY --from=wkhtmltopdf /bin/wkhtmltoimage /bin/wkhtmltopdf /bin/libwkhtmltox* /bin/

To make sure Sylius can find the binaries, we need to configure the paths in the respective environment variables, e.g., in the .env file:

WKHTMLTOPDF_PATH=/bin/wkhtmltopdf
WKHTMLTOIMAGE_PATH=/bin/wkhtmltoimage