Convert HTML to PDF in Azure App Service on Linux

6 Dec 20248 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 on Linux.

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).

Steps to convert HTML to PDF in Azure App Service on Linux

Step 1: Create a new ASP.NET Core MVC application.
Convert HTMLToPDF Azure NetCore Step1

Step 2: Choose your project’s target framework and select Configure for HTTPS.
Choose project target framework

Step 3: Install the Syncfusion.HtmlToPdfConverter.Net.Linux NuGet package as a reference to your .NET Core application NuGet.org.
NuGet package installation

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.

There are two ways to install the dependency packages to Azure server,

  • Using SSH from Azure portal.
  • By running the commands from C#.

3.1. Using SSH command line

  1. After publishing the Web application, login to the Azure portal in a web interface and open the published app service. Under Development Tools Menu, Open the SSH and Click the go link.
    Convert HTMLToPDF Azure NetCore Step4

  2. From the terminal window, you can install the dependency packages. Use the following single command to install all dependencies packages.

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

    NOTE

    By following the above steps 3.1, we need to install dependencies through SSH all the time after publishing the web application.

    3.2. Running the commands from C#

    1. Create a shell file with the above commands in the project and name it as dependenciesInstall.sh. In this article, these steps have been followed to install dependencies packages.
      Convert HTMLToPDF Azure NetCore Step5

    2. Set Copy to Output Directory as “Copy if newer” to the dependenciesInstall.sh file.
      Convert HTMLToPDF Azure NetCore Step6

    3. Include the following code snippet to install the dependencies code in Configure method in startup.cs file.

  • C#
  • //Install the dependencies packages for HTML to PDF conversion in Linux
    string shellFilePath = System.IO.Path.Combine(env.ContentRootPath, "dependenciesInstall.sh");
    InstallDependecies(shellFilePath);
  • C#
  • private void InstallDependecies(string shellFilePath) 
    {
          Process process = new Process
          {
                StartInfo = new ProcessStartInfo
                {
                      FileName = "/bin/bash",
                      Arguments = "-c " + shellFilePath,
                      CreateNoWindow = true,
                      UseShellExecute = false,
                 }
          };
          process.Start();
          process.WaitForExit();
    }

    NOTE

    By following the above steps 3.2, we can deploy application in Azure app service Linux and no need to install dependencies through SSH.

    Step 4: Add an Export to the PDF button in the index.cshtml.

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

    Step 5: Include the following namespaces.

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

    Step 6: 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()
    {
        Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "Development");
        //Install the dependencies packages for HTML to PDF conversion in Linux
        string shellFilePath = System.IO.Path.Combine(env.ContentRootPath, "dependenciesInstall.sh");
        InstallDependecies(shellFilePath);
        //Initialize HTML to PDF converter 
        HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
        BlinkConverterSettings settings = new BlinkConverterSettings();
        //Set command line arguments to run without sandbox.
        settings.CommandLineArguments.Add("--no-sandbox");
        settings.CommandLineArguments.Add("--disable-setuid-sandbox");
        //Assign BlinkConverter settings to the HTML converter 
        htmlConverter.ConverterSettings = settings;
        //Convert HTML string to PDF
        PdfDocument document = htmlConverter.Convert("http://www.syncfusion.com");
        //Save the document into stream
        MemoryStream stream = new MemoryStream();
        document.Save(stream);
        stream.Position = 0;
        //Close the document
        document.Close(true);
        //Defining the ContentType for pdf file
        string contentType = "application/pdf";
        //Define the file name
        string fileName = "URL_to_PDF.pdf";
        //Creates a FileContentResult object by using the file contents, content type, and file name
        return File(stream, contentType, fileName);
    }
    
    private void InstallDependecies(string shellFilePath)
    {
        Process process = new Process
        {
            StartInfo = new ProcessStartInfo
            {
                FileName = "/bin/bash",
                Arguments = "-c " + shellFilePath,
                CreateNoWindow = true,
                UseShellExecute = false,
            }
        };
        process.Start();
        process.WaitForExit();
    }

    Steps to publish as Azure App Linux

    Step 1: Right-click the project and select Publish.
    Convert HTMLToPDF Azure NetCore Step7

    Step 2: Create a new profile in publish target window.
    Convert HTMLToPDF Azure NetCore Step8

    Step 3: Create App service using Azure subscription and select a hosting plan.
    Convert HTMLToPDF Azure NetCore Step9

    Step 4: HTML to PDF conversion works from basic hosting plan (B1 to P3). So, select the hosting plan as required. HTML to PDF conversion will not work if the hosting plan is Free/Shared.
    Convert HTMLToPDF Azure NetCore Step10

    Step 5: After creating a profile, click the publish button.
    Convert HTMLToPDF Azure NetCore Step11

    Now, the published webpage will open in the browser. Click Export to PDF button to convert Syncfusion® webpage to PDF document.
    Convert HTMLToPDF Azure NetCore Step12

    A complete work sample for converting an HTML to PDF in Azure App service on Linux 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.