Convert HTML to PDF file in Linux Docker container

8 Aug 20234 minutes to read

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

Steps to convert HTML to PDF in Linux Docker container

Step 1: Create a new ASP.NET Core application and enable the Docker support with Linux as a 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
Convert HTMLToPDF Docker Step3


Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, you also have to add “Syncfusion.Licensing” assembly reference and include a license key in your projects. Please refer to this link to know about registering Syncfusion license key in your application to use our components.

Step 3: Include the following commands in the Docker file to install the dependent packages in the docker container.

  • C#
  • 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.

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

    Convert HTMLToPDF Docker Step5

    Step 5: A default controller with name HomeController.cs gets added on creation of ASP.NET Core project. Include the following namespaces in that HomeController.cs file.

  • C#
  • using Syncfusion.HtmlConverter;
    using Syncfusion.Pdf;
    using System.IO;

    Step 6: Add a new action method in HomeController.cs and include the below code snippet to convert HTML to PDF document using Convert method in HtmlToPdfConverter class. The HTML content will be scaled based on the given ViewPortSize property of BlinkConverterSettings class.

  • C#
  • public ActionResult ExportToPDF()
       //Initialize HTML to PDF converter. 
       HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter(); 
       BlinkConverterSettings settings = new BlinkConverterSettings();     
       //Set command line arguments to run without the sandbox.
       //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(""); 
       //Create memory stream.
       MemoryStream stream = new MemoryStream(); 
       //Save the document to memory stream. 
       return File(stream.ToArray(), System.Net.Mime.MediaTypeNames.Application.Pdf, "HTML-to-PDF.pdf");

    Step 7: Build and run the sample in the Docker. It will pull the Linux Docker image from the Docker hub and run the project. Now, the webpage will open in the browser. Click the button to convert the webpage to a PDF document.

    By executing the program, you will get the PDF document as follows.
    Convert HTMLToPDF Dockeroutput

    A complete working sample for converting an HTML to PDF in the Linux docker container can be downloaded from Github.

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

    An online sample link to convert HTML to PDF document in ASP.NET Core.