Convert HTML to PDF in Azure App Service Linux with docker

20 Jan 20255 minutes to read

The Syncfusion® 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 an HTML to PDF using C# with the Blink rendering engine in Azure App Service Linux with docker.

NOTE

HTML to PDF converter is not supported with Azure App Service windows. We internally use Blink rendering engine for the conversion, it uses GDI calls for viewing and rendering the webpages. But Azure app service blocks GDI calls in the Azure website environment. As the Azure website does not have the elevated permission and enough rights, we can not launch the Chrome headless browser in the Azure app service windows (Azure website and Azure function).

Step 1: Create a new ASP.NET Core application and enable the docker support with Linux as a target OS.
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 NuGet.org.

NOTE

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.

Convert HTMLToPDF Azure Docker Step

Step 4: 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 Azure Docker Step4

    Step 5: Add a new button in the Index.cshtml file.

  • C#
  • <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: Include the following namespaces.

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

    Step 7: Add the code samples in the controller to convert HTML to PDF document using Convert method in HtmlToPdfConverter class. The Blink command line arguments based on the given CommandLineArguments 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.
        settings.CommandLineArguments.Add("--no-sandbox");
        settings.CommandLineArguments.Add("--disable-setuid-sandbox");
        //Assign Blink settings to the HTML converter.
        htmlConverter.ConverterSettings = settings;
        //Convert URL to PDF.
        PdfDocument document = htmlConverter.Convert("https://www.syncfusion.com");
        MemoryStream stream = new MemoryStream();
        //Save and close a PDF document. 
        document.Save(stream);
        return File(stream.ToArray(), System.Net.Mime.MediaTypeNames.Application.Pdf, "URL_to_PDF.pdf");
    }

    Step 8: Build and run the sample in docker, it will pull the Linux docker image from the docker hub and run the project. Now, the webpage will open in the browser and click the button to convert the Syncfusion® webpage to a PDF.
    Convert HTMLToPDF Azure Docker Step6

    By executing the program, you will get the PDF document as follows.
    HTML to PDF output document

    Deploy the 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: It 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, it will open the Azure website in the browser.
    Convert HTMLToPDF Azure Docker button

    Step 6: Click the button to convert Syncfusion® webpage to a PDF document. You will get the PDF document as follows.
    Convert HTMLToPDF Azure Docker Output

    A complete work sample 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.