Convert HTML to PDF in Azure App Service Linux with Docker

20 Jul 20266 minutes to read

The HTML to PDF converter is a .NET Core library for converting webpages, SVG, MHTML, and HTML to PDF using C#. The result preserves all graphics, images, text, fonts, and the layout of the original HTML document or webpage. Using this library, you can convert HTML to PDF using C# with the Blink rendering engine in Azure App Service Linux with Docker.

Prerequisites

Version Compatibility

The Syncfusion.HtmlToPdfConverter.Net.Linux NuGet package uses the Blink rendering engine for HTML to PDF conversion. This library is compatible with .NET 8.0 and later versions

Supported Inputs

The HTML to PDF converter supports the following input types:

  • HTML String: Direct HTML content.
  • URL: Web pages and online HTML content.
  • HTML Files: Local HTML files.
  • MHTML Files: Web archive (.mhtml/.mht) content.
  • Authenticated Web Pages: Pages that require cookies, form authentication, or HTTP authentication.
  • HTTP GET/POST Requests: HTML content accessed through GET or POST methods

Required Software

  • .NET 8 SDK or later
  • Linux x86_64 environment

Register the license key

NOTE

Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or from the NuGet feed, you must add the “Syncfusion.Licensing” assembly reference and register a license key in your application. Please refer to this link for details on registering a Syncfusion® license key.

Include a license key in your HomeController.cs file before creating an HtmlToPdfConverter instance. Refer to the Syncfusion License documentation to learn about registering the Syncfusion license key in your application.

using Syncfusion.Licensing;

public class HomeController : Controller
{
    public HomeController()
    {
        // Register the Syncfusion license
        SyncfusionLicenseProvider.RegisterLicense("YOUR LICENSE KEY");
    }
}

NOTE

Starting from version 29.2.4, it is no longer necessary to manually add the following command-line arguments when using the Blink rendering engine:

settings.CommandLineArguments.Add("--no-sandbox");
settings.CommandLineArguments.Add("--disable-setuid-sandbox");

These arguments are only required when using older versions of the library that depend on Blink in sandbox-restricted environments.

Step 1: Create a new ASP.NET Core application and enable Docker support with Linux as the target operating system.
Convert HTMLToPDF Azure Docker Step1

Step 2: Choose your project’s target framework, select Configure for HTTPS and enable Docker.
Convert HTMLToPDF Azure Docker Step2

Step 3: Install the Syncfusion.HtmlToPdfConverter.Net.Linux NuGet package as a reference to your .NET Core application from NuGet.org.
Convert HTMLToPDF Azure Docker Step

Step 4: Add the following commands to the Dockerfile to install the dependency packages required for Blink rendering in the Docker container:

# Update package manager and install all required dependencies for Blink rendering engine
RUN apt-get update && \
apt-get install -yq --no-install-recommends \ 
libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 \ 
libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 \ 
libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 \ 
libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 \ 
libnss3 libgbm1

Convert HTMLToPDF Azure Docker Step4

Step 5: Add a new button in the Index.cshtml file to trigger HTML to PDF conversion:

<div class="btn">
    @{ Html.BeginForm("ExportToPDF", "Home", FormMethod.Post);
        {
            <input type="submit" value="Export To PDF" class=" btn" />
        }
     }
 </div>

Convert HTMLToPDF Azure Docker Step5

Step 6: Add the following namespaces to the HomeController.cs file:

using Syncfusion.HtmlConverter;
using Syncfusion.Pdf;

Step 7: Add the code samples to the HomeController to convert HTML to PDF document using the Convert method in the HtmlToPdfConverter class with BlinkConverterSettings:

public ActionResult ExportToPDF()
{
    // Initialize HTML to PDF converter with default Blink rendering engine
    HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
    // Create Blink converter settings for Docker container environment
    BlinkConverterSettings settings = new BlinkConverterSettings();
    // Assign converter settings to the HTML converter instance
    htmlConverter.ConverterSettings = settings;
    // Convert URL to PDF document using Blink rendering engine
    PdfDocument document = htmlConverter.Convert("https://www.syncfusion.com");
    // Create memory stream to store the converted PDF bytes
    MemoryStream stream = new MemoryStream();
    // Save PDF document to memory stream
    document.Save(stream);
    // Return PDF file as file download response to the browser
    return File(stream.ToArray(), System.Net.Mime.MediaTypeNames.Application.Pdf, "URL_to_PDF.pdf");
}

Step 8: Build and run the sample in Docker. Docker will pull the Linux image from Docker Hub and run the project. The webpage will open in your browser. Click the button to convert the Syncfusion® webpage to a PDF.
Convert HTMLToPDF Azure Docker Step6

By executing the program, you will obtain the following PDF document output:
HTML to PDF output document

Deploy the Docker container to Azure Container Instance

Step 1: Create a publish target to deploy the Docker image to Azure.
Convert HTMLToPDF Azure Docker Step8

Step 2: Create Azure App Service with resource group, hosting plan, and container registry.
Convert HTMLToPDF Azure Docker Step9

Step 3: Publish the Docker image to Azure Container Instance.
Convert HTMLToPDF Azure Docker Step10

Step 4: The deployment process will push the Docker image to the Azure Container Registry and deploy it to the Azure Container Instance.
Convert HTMLToPDF Azure Docker Step11

Step 5: After successful deployment, the Azure website will open in your browser.
Convert HTMLToPDF Azure Docker button

Step 6: Click the button to convert the Syncfusion® webpage to a PDF document. You will obtain the following PDF document output:
Convert HTMLToPDF Azure Docker Output

A complete working sample for converting HTML to PDF in Azure App Service with Docker can be downloaded from GitHub.

Click here to explore the rich set of Syncfusion® HTML to PDF converter library features.

You can also view the online sample to convert HTML to PDF documents in ASP.NET Core.