3

I have an AWS Lambda function that generates PDFs using the html-pdf library with custom fonts.

At first, I imported my fonts externally from Google Fonts, but then the PDF's size has enlarged by ten times.

So I tried to import my fonts locally src('file:///var/task/fonts/...ttf/woff2') but still no luck.

Lastly, I trie to create fonts folder in the main project and then I added all of my fonts, plus the file fonts.config:

<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>
  <dir>/var/task/fonts/</dir>
  <cachedir>/tmp/fonts-cache/</cachedir>
  <config></config>
</fontconfig>

and set the following env:

FONTCONFIG_PATH = /var/task/fonts

but still no luck (I haven't installed fontconfig since I'm not sure how and if I need to).

My Runtime env is Node.js 8.1.0.

Eliya Cohen
  • 10,716
  • 13
  • 59
  • 116
  • Not an answer just just to let you know, Node.js 8.1.0 is EOL as of Dec 31, 2019 https://docs.aws.amazon.com/lambda/latest/dg/runtime-support-policy.html – Ethan Harris Dec 17 '19 at 14:38

1 Answers1

1

You can upload your fonts into an S3 bucket and then download them to the lambda's /tmp directory, during its execution. In case your lib creates .pkl files, you should first change your root directory to /tmp (lambda is not allowed to write in the default root directory). The following Python code downloads your files from a /fonts directory in an S3 bucket to /tmp/fonts "local" directory.

import os
import boto3

os.chdir('/tmp')
os.mkdir(os.path.join('/tmp/', 'fonts'))
s3 = boto3.resource('s3')
s3_client = boto3.client('s3')
my_bucket = s3.Bucket("bucket_name")

for file in my_bucket.objects.filter(Prefix="fonts/"):

    filename = file.key
    short_filename = filename.replace('fonts/','')
    if(len(short_filename) > 0):
        s3_client.download_file(
            bucket,
            filename,
            "/tmp/fonts/" + short_filename,
        )