Convert HTML to PDF in Azure Functions and Azure App Services

Docotic.Pdf provides free add-on for HTML to PDF conversion in .NET. The add-on uses Chromium during conversion. That helps to get great conversion quality. However, there is a drawback - you need permissions to run Chromium in a target environment.

Usually, you have complete control over dedicated or virtual servers. But serverless or sandboxed environments like Azure Functions or AWS Lambda are a different story. It might be challenging to install dependencies or run Chromium there.

Since version 8.1.12268-dev, you can use the HTML to PDF add-on for Docotic.Pdf library in Azure Functions and Azure App Services on Linux.

Run HTML to PDF conversion in Azure on Linux

Azure Functions and App Services run in a sandbox. You would need to do the following to convert HTML to PDF in Azure on Linux:

  1. Pre-install Chromium dependencies
  2. Make add-on run Chromium with disabled sandbox
  3. Make add-on download browser to a custom path

Install Chromium dependencies on Linux

Since August 2021, Linux environments for Azure Functions and Azure App Services come with pre-installed Chromium dependencies. So, you do not need to install anything.

If necessary, you can use the following commands to install Chromium dependencies in different Linux distributives.

# Debian
apt-get -y install chromium

# CentOS
yum -y install pango.x86_64 libXcomposite.x86_64 libXcursor.x86_64 libXdamage.x86_64 libXext.x86_64 libXi.x86_64 libXtst.x86_64 cups-libs.x86_64 libXScrnSaver.x86_64 libXrandr.x86_64 GConf2.x86_64 alsa-lib.x86_64 atk.x86_64 gtk3.x86_64 ipa-gothic-fonts xorg-x11-fonts-100dpi xorg-x11-fonts-75dpi xorg-x11-utils xorg-x11-fonts-cyrillic xorg-x11-fonts-Type1 xorg-x11-fonts-misc

Make add-on run Chromium with disabled sandbox

You will get the error "Failed to launch browser! Running as root without --no-sandbox is not supported" if you run the default HTML to PDF conversion code in Azure on Linux:

using (var converter = await HtmlConverter.CreateAsync())
    ...

Set PdfEngineOptions.UseSandbox property to false and add-on will launch Chromium without the sandbox:

var engineOptions = new HtmlEngineOptions
{
    UseSandbox = false
};
using (var converter = await HtmlConverter.CreateAsync(engineOptions))
{
    using (var pdf = await converter.CreatePdfAsync(..))
        pdf.Save("output.pdf");
}

Note that disabling Chromium sandbox is not recommended. When the sandbox is disabled, Chromium can potentially execute malicious Javascript-based exploits. In our case, Chromium runs in the Azure sandbox, which reduces the risk of attack. You can do the following to reduce risks more:

  • Process local HTML templates and trusted websites only.
  • Run HTML to PDF conversion in Azure Functions/App Services isolated from other services.

Make add-on download browser to a custom path

Azure Functions and App Services often run directly from a ZIP package. In such cases, you should specify a path where the add-on will put the downloaded Chromium. This is required because the add-on cannot download Chromium to the application directory or launch it from there. Use PdfEngineOptions.Path property to specify a custom path for the downloaded Chromium. This sample C# code shows how to download Chromium to the current user's temporary folder:

var engineOptions = new HtmlEngineOptions
{
    Path = System.IO.Path.GetTempPath(),
    UseSandbox = false
};
using (var converter = await HtmlConverter.CreateAsync(engineOptions))
{
    using (var pdf = await converter.CreatePdfAsync(..))
        pdf.Save("output.pdf");
}

That's it! Use the code above to convert HTML to PDF in Azure on Linux using the Docotic.Pdf.HtmlToPdf add-on.

Run HTML to PDF conversion in Azure on Windows

Currently (August 2021), Azure Functions and Azure App Services on Windows do not allow running Chromium. You will get the following error on launch: "System.ComponentModel.Win32Exception (14001): The application has failed to start because its side-by-side configuration is incorrect. Please see the application event log or use the command-line sxstrace.exe tool for more detail". Thus, Azure Functions and Azure App Services on Windows are not supported by the add-on at the moment.

Convert HTML to PDF in a Docker container

There is another option to use the HTML to PDF add-on in Azure. You can run the conversion in a Docker container. Just install dependencies in a Dockerfile and disable the Chromium sandbox in C# code. Here is a sample Dockerfile for HTML to PDF conversion in an ASP.NET Core application:

FROM mcr.microsoft.com/dotnet/aspnet:3.1 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

RUN apt-get update && apt-get install -y chromium

FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build
WORKDIR /src
COPY ["HtmlToPdfWebApp/HtmlToPdfWebApp.csproj", "HtmlToPdfWebApp/"]
RUN dotnet restore "HtmlToPdfWebApp/HtmlToPdfWebApp.csproj"
COPY . .
WORKDIR "/src/HtmlToPdfWebApp"
RUN dotnet build "HtmlToPdfWebApp.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "HtmlToPdfWebApp.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "HtmlToPdfWebApp.dll"]

And here is a sample Dockerfile for a similar Azure Function app:

FROM mcr.microsoft.com/azure-functions/dotnet:3.0 AS base
WORKDIR /home/site/wwwroot
EXPOSE 80

RUN apt-get update && apt-get install -y chromium

FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build
WORKDIR /src
COPY ["HtmlToPdfFunction/HtmlToPdfFunction.csproj", "HtmlToPdfFunction/"]
RUN dotnet restore "HtmlToPdfFunction/HtmlToPdfFunction.csproj"
COPY . .
WORKDIR "/src/HtmlToPdfFunction"
RUN dotnet build "HtmlToPdfFunction.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "HtmlToPdfFunction.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /home/site/wwwroot
COPY --from=publish /app/publish .
ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
    AzureFunctionsJobHost__Logging__Console__IsEnabled=true