Using EvoPdf Chromium for .NET in Azure Functions on Linux

EvoPdf Chromium for .NET is a library that can be easily integrated into Azure Functions for Linux to convert web pages and HTML strings into PDF or image formats.

The HTML to PDF conversion component uses a Chromium-based rendering engine capable of handling modern HTML, CSS and JavaScript in compliance with the latest web standards.

Platform Compatibility

EvoPdf Chromium for .NET is compatible with 64-bit applications hosted on Azure Functions for Linux. Before running your application, additional system packages must be installed in the Azure environment. The next sections provide detailed setup instructions.

The library targets .NET Standard 2.0, allowing it to be used in any .NET Core application that supports this standard.

Create a .NET Application for Azure Functions on Linux

Create a new .NET project in Visual Studio and use the NuGet Package Manager to add a reference to the EvoPdf.Chromium.Linux package.

After installing the package, add the using EvoPdf.Chromium; directive at the top of your source files to access the EvoPdf Chromium API.

Code examples and usage details can be found in the Getting Started on Linux documentation section.

Prepare the Linux Environment

The EvoPdf converter requires several native Linux libraries. These must be installed in the Azure App Service environment before running your application.

Deploying to Azure Functions on Linux is more challenging than to Azure App Service. The file system is read-only except for certain directories such as /tmp and runtime files are deployed without execution permissions by default.

The EvoPdf Chromium library provides an API to install the required dependencies and perform the necessary configuration steps.

Configure the Environment via Installation Code in Your .NET Application

The easiest approach is to call the ConfigureRuntime(Boolean, String, String, Boolean) method in your .NET application to configure the runtime for Azure Functions on Linux.

This method allows you to specify a Bash script that will be executed before the first HTML to PDF or image conversion. It tracks whether the script has already run and ensures it runs again after the application restarts.

You can choose to install only the required libraries or include optional packages, such as Microsoft Core Fonts, to ensure consistent rendering when using fonts commonly found on Windows platforms.

Install Only the Required Dependency Packages

To install only the required dependency packages, add the following line of code before the first call to the converter. You can call the method multiple times, but only the first call sets up the configuration. Subsequent calls will be ignored.

C#
Installation.ConfigureRuntime(true, null, "apt update && apt install -y libnss3 libatk1.0-0 libatk-bridge2.0-0 libcups2 libgbm1 libxkbcommon0 libpango-1.0-0 libcairo2 libasound2");

Install Both Required Dependencies and Microsoft Core Fonts (Optional)

If your HTML content uses fonts commonly found on Windows systems, installing Microsoft Core Fonts is recommended to ensure consistent rendering.

To install both the required packages and Microsoft Core Fonts, add the following line of code before the first call to the converter:

C#
Installation.ConfigureRuntime(true, null, "apt update && apt install -y libnss3 libatk1.0-0 libatk-bridge2.0-0 libcups2 libgbm1 libxkbcommon0 libpango-1.0-0 libcairo2 libasound2 && wget -O /tmp/ttf-mscorefonts-installer_3.8_all.deb http://ftp.de.debian.org/debian/pool/contrib/m/msttcorefonts/ttf-mscorefonts-installer_3.8_all.deb && apt install -y /tmp/ttf-mscorefonts-installer_3.8_all.deb fontconfig && fc-cache -f -v");

Publish Your Application to Azure Functions on Linux

HTML to PDF conversion can be resource-intensive, depending on the complexity of the content. Proper sizing of the hosting plan is important.

When publishing, select the App Service plan or Premium as the Plan Type in the publish profile wizard in Visual Studio. The Consumption plan is not suitable for running the converter.

The minimum supported hosting plan is B2 (2 cores, 3.5 GB RAM). The Free plan is also not suitable for Azure Functions running the converter.

For production scenarios or heavy workloads, a Premium plan such as P1v3 (2 vCPUs, 8 GB RAM) or higher is strongly recommended.

When configuring your publish profile, select Portable as the target runtime.

Finally, publish the application to Azure Functions on Linux.

Alternative: Manually Set Up the Environment via SSH Console

Although configuring the environment via .NET code is the preferred method for consistency across restarts, you can alternatively install the required dependencies manually using the SSH console. Follow the steps below to configure the Linux environment for Azure Functions.

Set HTML Loader Path

Before publishing your application, set the full path to the evopdf_loadhtml executable in your .NET code. This should point to a writable location, such as /tmp, where the runtime files will be copied later.

C#
converter.HtmlLoaderFilePath = "/tmp/runtimes/linux-x64/native/evopdf_loadhtml";

Publish the Azure Function on Linux

Publish your Azure Function using the same deployment options previously described for the .NET code-based configuration method.

Once the application is published, continue with the environment setup to install the required packages, as outlined in the steps below.

Install Required Linux Packages via SSH

Open the Azure Portal, navigate to your Azure Function and go to Development Tools -> SSH. Click the "Go" button to open the SSH console.

Run the following command to install the required Linux packages:

bash
apt update && apt install -y libnss3 libatk1.0-0 libatk-bridge2.0-0 libcups2 libgbm1 libxkbcommon0 libpango-1.0-0 libcairo2 libasound2

Install Microsoft Core Fonts (Optional)

If your HTML content uses fonts commonly found on Windows systems, it is recommended to install Microsoft Core Fonts using the following command:

bash
wget -O /tmp/ttf-mscorefonts-installer_3.8_all.deb http://ftp.de.debian.org/debian/pool/contrib/m/msttcorefonts/ttf-mscorefonts-installer_3.8_all.deb && apt install -y /tmp/ttf-mscorefonts-installer_3.8_all.deb fontconfig && fc-cache -f -v

Copy Runtime and Grant Execution Permission

Copy the runtime files to the /tmp directory:

bash
cp -r /home/site/wwwroot/runtimes /tmp/runtimes

Then, grant execution permission to the loader binary:

bash
chmod +x /tmp/runtimes/linux-x64/native/evopdf_loadhtml

Run the Application

With all dependencies installed and configurations completed, your application is ready to run. You can also follow the same procedure to publish and test the EvoPdf ASP.NET demo application in Azure App Service for Linux.

See Also