Convert HTML to PDF file in Linux Docker container

20 Jul 20264 minutes to read

The HTML to PDF converter is a .NET library that converts HTML or web pages to PDF documents in a Linux Docker container.

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.0 or later
  • Visual Studio 2022 or later
  • Docker installed locally

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
{
    // 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.

Steps to convert HTML to PDF in Linux Docker container

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

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

Step 3: Add the following commands to the Dockerfile to install Blink rendering dependencies in the Docker container:

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 Docker Step4

Step 4: Add a new button in the index.cshtml as shown below.

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

Convert HTMLToPDF Docker Step5

Step 5: A default controller named HomeController.cs is added when creating the ASP.NET Core project. Add the following namespaces to that HomeController.cs file:

using Syncfusion.HtmlConverter;
using Syncfusion.Pdf;

Step 6: Add a new action method in HomeController.cs to convert HTML to PDF using the Convert method in the HtmlToPdfConverter class with BlinkConverterSettings:

public ActionResult ExportToPDF()
{
   //Initialize HTML to PDF converter. 
   HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter(); 
   BlinkConverterSettings settings = new BlinkConverterSettings();     
   //Set Blink viewport size.
   settings.ViewPortSize = new Syncfusion.Drawing.Size(1280, 0);     
   //Assign Blink settings to the HTML converter.
   htmlConverter.ConverterSettings = settings; 
   //Convert URL to PDF document.
   PdfDocument document = htmlConverter.Convert("https://www.syncfusion.com"); 
   //Create memory stream.
   MemoryStream stream = new MemoryStream(); 
   //Save the document to memory stream. 
   document.Save(stream); 
   return File(stream.ToArray(), System.Net.Mime.MediaTypeNames.Application.Pdf, "HTML-to-PDF.pdf");
}

Step 7: Build and run the sample in Docker. Docker will pull the Linux Docker image from Docker Hub and run the project. The webpage will open in the browser. Click the Export To PDF button to convert the webpage to a PDF document.

By executing the program, Docker will generate and display the following PDF document:
Convert HTMLToPDF Dockeroutput

A complete working sample for converting HTML to PDF in a Linux Docker container 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.