Using phantom js on lambda with nodejs 10 and 12 is not directly possible, it becase it misses few dependencies like libfontconfig.so on the newer amazon linux 2 base image. To overcome this problem you have to use your own fonts folder which contains the required files and a font.conf file
More details are contained in the links given below, you can generate your own or clone this pack and use it with your lambda
- Clone the pack, and add the
fonts
folder to your lambda zip. - add following environment variables to you lambda
process.env['FONTCONFIG_PATH'] = path.join(process.env['LAMBDA_TASK_ROOT'], 'fonts');
OR
process.env['FONTCONFIG_PATH'] = path.join(process.env['LAMBDA_TASK_ROOT'], '...');
AND
process.env['LD_LIBRARY_PATH'] = path.join(process.env['LAMBDA_TASK_ROOT'], 'fonts');
OR
process.env['LD_LIBRARY_PATH'] = path.join(process.env['LAMBDA_TASK_ROOT'], '...');
- if you are using lambda layers, the set the env variables as following
FONTCONFIG_PATH='/opt/fonts'
LD_LIBRARY_PATH='/opt/fonts'
here the fonts
folder is the root directory of you layer
- Change the font.conf file with following content:
<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>
<dir>/var/task/fonts/</dir>
<cachedir>/tmp/fonts-cache/</cachedir>
<config></config>
</fontconfig>
- If using lambda layer, with folder structure for example:
nodejs/fonts/font.conf
, Change the font.conf as with following content
<fontconfig>
<dir>/opt/nodejs/fonts/</dir>
<cachedir>/tmp/fonts-cache/</cachedir>
<config></config>
</fontconfig>
If you face any more isses, such as PDF getting generated without text, then add .ttf font files to the fonts folder in the lambda/layer zip
- Go to /usr/share/fonts/#{FONT FOLDER NAME}, copy all the font files from the folder folder
- if running on docker copy using
docker cp <CONTAINER_ID>:/fonts/ .
- https://medium.com/@anjanava.biswas/nodejs-runtime-environment-with-aws-lambda-layers-f3914613e20e
- https://stackoverflow.com/a/56843029/3556874
- https://stackoverflow.com/a/18101158/3556874
- https://tech.mybuilder.com/compiling-wkhtmltopdf-aws-lambda-with-bref-easier-than-you-think/#comment-4681622370