Fabio Baldissera
Fabio Baldissera

Reputation: 593

Can I use wkhtmltopdf on AWS Lambda

I'm using a AWS Lambda function to merge PDF files stored on S3. In addition to that, I need now to create a PDF file (from HTML and CSS). I was trying to use wkhtmltopdf, but it seems that I would have to install it using apt-get install (which I don`t think I have access on AWS Lambda).

Any ideas on how can I do it?

Any suggestions for wkhtmltopdf replacements?

Thanks!

Upvotes: 19

Views: 22536

Answers (7)

trazoM
trazoM

Reputation: 324

I had challenges getting wkhtmltopdf to work on AWS Lambda with the Python runtime. The main problem with the current official wkhtmltopdf is that it there are lots of .so files missing. Thanks to @haz's answer, I was able to compile a working set of binaries tailored for the Docker Lambda Python runtime. I’ve made the exact working binaries available here:

https://github.com/Chuukwudi/wkhtmltox_for_lambda.

To use these binaries in your Lambda container, your Dockerfile should look like this:

FROM public.ecr.aws/lambda/python:3.12

# Set environment variables for font and library paths
ENV FONTCONFIG_PATH="/opt/fonts"
ENV LD_LIBRARY_PATH="/opt/lib/"

# Copy wkhtmltox binaries to the appropriate directory in the image
COPY wkhtmltox/. /opt/
RUN chmod +x /opt/bin/wkhtmltopdf && \
    chmod -R 755 /opt/lib && \
    chmod -R 755 /opt/fonts

If you're using pdfkit, ensure you set the wkhtmltopdf_path configuration to point to the binary:

import pdfkit
pdfkit.configuration(wkhtmltopdf='/opt/bin/wkhtmltopdf')

This setup is static and should work seamlessly for generating PDFs on AWS Lambda with the Python runtime for a long time.

Important caveat: If you set up CI CD on github actions with these, ensure that your gitignore doesn't contain /lib otherwise the lib folder will be omitted.

Upvotes: 0

ozgeneral
ozgeneral

Reputation: 6819

I ran into this on python lambdas, adding below to Dockerfile was sufficient:

FROM public.ecr.aws/lambda/python:3.12

# note that this is a minimal container image
# (https://docs.aws.amazon.com/linux/al2023/ug/minimal-container.html)
# so dnf is microdnf, etc.

# install other dependencies that'll be needed
RUN dnf install -y fontconfig freetype urw-fonts libjpeg xorg-x11-server-Xvfb xorg-x11-fonts-Type1 xorg-x11-fonts-75dpi
RUN dnf install -y libXrender libXext libX11 libXau libpng openssl openssl-libs wget which

# install openssl1.1 as libssl.so.1.1 seems to be needed
RUN wget https://mirror.stream.centos.org/9-stream/AppStream/x86_64/os/Packages/compat-openssl11-1.1.1k-3.el9.x86_64.rpm
RUN rpm -ivh compat-openssl11-1.1.1k-3.el9.x86_64.rpm

# install wkhtmltopdf from source
RUN wget https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6-1/wkhtmltox-0.12.6-1.centos8.x86_64.rpm
RUN rpm -ivh wkhtmltox-0.12.6-1.centos8.x86_64.rpm

# confirm it works:
RUN wkhtmltopdf --version

...

CMD [ "app.lambda_handler" ]

fun fact, if you are using pdfkit, even after you install wkhtmltopdf it doesn't seem to work at first, because pdfkit tries to find the binary using "which wkhtmltopdf", but the command "which" doesn't exist in minimal python image, so you also have to install that for things to go smooth (included in the dependencies above) :)

Upvotes: 1

pixelistik
pixelistik

Reputation: 7830

There is a specific download of wkhtmltopdf for use with AWS Lambda.

It is meant to be used as a separately uploaded Layer.

Upvotes: 10

haz
haz

Reputation: 1636

Important caveat: If you want to use wkhtmltopdf successfully on AWS Lambda, you will need to use v0.12.4.

It turns out that newer versions have some issues with dynamic libraries. If you drill into it, you'll find some .so files missing.

Source: Richard Keller

Upvotes: 4

Fish
Fish

Reputation: 459

Include the wkhtmltopdf binary and making sure it has execution permission(chmod 755). Add the binary path to your language runtime. e.g. with nodejs

process.env['PATH'] = process.env['PATH'] + ':' + process.env['LAMBDA_TASK_ROOT'] + '/bin/linux';

Upvotes: 10

Björn
Björn

Reputation: 3428

There are multiple projects on GitHub claiming to run wkhtmltopdf on Lambda. Here are a few.

Upvotes: 15

Victor Rojas
Victor Rojas

Reputation: 91

Upload the binary inside a folder of your project for example in a folder "binary /" so that at the time of lambda execution you can call it by reference

Upvotes: 6

Related Questions