wkhtmltopdf with php:8-fpm-alpine

9.3k Views Asked by At

I have an existing php:8-fpm-alpine Dockerfile, and i need to add WKHTMLTOPDF package. Is that even possible. I tried using following dockerfile, but i get following error log:

Dockerfile...

FROM php:8-fpm-alpine
...
RUN apk add xvfb libfontconfig wkhtmltopdf

error:

ERROR [ 8/13] RUN apk add --no-cache wkhtmltopdf                                                                                                   2.1s

[ 8/13] RUN apk add --no-cache wkhtmltopdf:
#12 0.567 fetch https://dl-cdn.alpinelinux.org/alpine/v3.15/main/x86_64/APKINDEX.tar.gz
#12 1.097 fetch https://dl-cdn.alpinelinux.org/alpine/v3.15/community/x86_64/APKINDEX.tar.gz
#12 2.001 ERROR: unable to select packages:
#12 2.034   wkhtmltopdf (no such package):
#12 2.034     required by: world[wkhtmltopdf]

executor failed running [/bin/sh -c apk add --no-cache wkhtmltopdf]: exit code: 1
ERROR: Service 'php' failed to build : Build failed

I tried including contents from following repository, but i think its way too much work for 1 package, and it breaks in build process: https://github.com/alloylab/Docker-Alpine-wkhtmltopdf

Any help would be appreciated.

3

There are 3 best solutions below

1
On BEST ANSWER

I faced a similar problem with php:7.4-fpm-alpine image.

It seems like wkhtmltopdf is missing in Alpine v.3.15, but it is available in v.3.14.

Try to change

FROM php:8-fpm-alpine

to

FROM php:8-fpm-alpine3.14
1
On

My modern approach using surnet/alpine-wkhtmltopdf image

Goals:

  • Use the newest alpine (3.17 here)
  • Use the newest php (8.2 here)
  • Have wkhtmltopdf anyway
  • Do not include legacy repositories

Dockerfile:

FROM surnet/alpine-wkhtmltopdf:3.16.2-0.12.6-full as wkhtmltopdf
FROM php:8.2-fpm-alpine3.17 AS app


# wkhtmltopdf install dependencies
RUN apk add --no-cache \
        libstdc++ \
        libx11 \
        libxrender \
        libxext \
        libssl1.1 \
        ca-certificates \
        fontconfig \
        freetype \
        ttf-droid \
        ttf-freefont \
        ttf-liberation \
        # more fonts
        ;

# wkhtmltopdf copy bins from ext image
COPY --from=wkhtmltopdf /bin/wkhtmltopdf /bin/libwkhtmltox.so /bin/


# install php extensions, apache/nginx etc.

  1. You can control version of alpine-wkhtmltopdf image
  2. You can control version of php and alpine
  3. You can download more fonts if you need

Partially based on manual

1
On

I had the same problem when trying to update to php:8.1.9-fpm-alpine3.16

To get this to work I added a link to the community 3.14 repository for wkhtmltopdf. It turned out it had some dependencies from the main repository also :

ERROR: unable to select packages:
  so:libicui18n.so.67 (no such package):
    required by: qt5-qtwebkit-5.212.0_alpha4-r14[so:libicui18n.so.67]
  so:libicuuc.so.67 (no such package):
    required by: qt5-qtwebkit-5.212.0_alpha4-r14[so:libicuuc.so.67]

So you need to add that aswell

# Install packages not yet updated for the current alpine version TODO remove when no longer needed
RUN echo 'https://dl-cdn.alpinelinux.org/alpine/v3.14/community' >> /etc/apk/repositories
RUN echo 'https://dl-cdn.alpinelinux.org/alpine/v3.14/main' >> /etc/apk/repositories
RUN apk add --no-cache wkhtmltopdf