Check installed fonts on linux based Node.js Azure Function

By Mirek on (tags: azure, Azure Function, fontconfig, linux, Node js, categories: azure, architecture, code)

As you may have read in one of my last post, I was struggling with Vega rendering function that deployed on Azure linux was lacking some fonts. In this post I’ll show you how to check the list of installed fonts on your linux based Azure function.

The scenario will be simillar to what we have with Vega rendering function.
We create an Azure, Node js function app that will be running linux operating system. The goal is to get the list of all fonts installed in the system.

There is a node package called font-list, which is ideal for that case. Hovewer as this package supports also other opearting systems, we will extract just the linux part and put that in our azure function code. We dont need the functionality to get list of fonts on windows based environment.

The code for the function will look like this:

module.exports = async function (context, req) {
    try {
        const exec = require('child_process').exec;
        const util = require('util');
        const pexec = util.promisify(exec);
        const cmd = `fc-list -f "'%{family[0]}'\\n"`;
        const { stdout } = await pexec(cmd, { maxBuffer: 1024 * 1024 * 10 })

        context.res = {
            body: stdout
        };
    }
    catch (error) {
        context.log(`Error during fetching font list: ${error}.`);
        context.res = {
            body: `Error during fetching font list: ${error}.`,
            status: 500
        };
    }
}

As clearly seen, it basically calls linux command fc-list, which returns list of all installed fonts in the system.
All is quite straight forward. When we get the function deployed to Azure it simply returns something like this:

'DejaVu Sans'
'DejaVu Sans'
'DejaVu Sans Mono'
'DejaVu Serif'
'DejaVu Serif'
'DejaVu Sans Mono'

There is one caveat though. As fc-list command is part of the fontconfig module, that module must be installed on the system and the font cache must be created. This is unfortunatelly, not a default case when we deploy Azure function on linux and when you try to run the function, you will probaly get an error message like:

Error: Command failed: fc-list

There is a way to overcome that, even on the Azure Function deployement. Go the function overview in the Azure portal and look for SSH in the Deployment Tools section on the left side toolbar

CheckFontsListApp 001

The llinux bash terminal should open allowing you to run linux commands right in deployed app function environment. Now simply run following command

apt-get -y install fontconfig

and that should do the job.

Thanks!