Articles in this section
Category / Section

How to convert HTML to PDF in Azure app service using the Blink with Linux docker?

2 mins read

The Syncfusion HTML to PDF converter is a .NET library for converting webpages, SVG, MHTML, and HTML to PDF using C#. It is reliable and accurate. 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 in Azure app service using the Blink with Linux docker container.

Steps to convert HTML to PDF in Azure app service using the Blink with Linux docker container:

  1. Create a new ASP.NET Core application and enable the docker support with Linux as a target OS.

Create new core MVC project  in ASP.NET Core PDF

 

Select NET Core 3.1 as target framework in ASP.NET Core PDF

 

  1. Install the Syncfusion.HtmlToPdfConverter.Net.Linux NuGet package as a reference to your .NET Core application from NuGet.org.

Install latest Syncfusion HTML to PDF Linux NuGet package as reference in ASP.NET Core PDF

  1. Include the following snippet in the docker file to install the dependent packages 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
    

 

Install the dependent packages in the docker container in ASP.NET Core PDF

 

  1. Add an Export To PDF button in the index.cshtml.
    <div class="btn">
      @{ Html.BeginForm("ExportToPDF", "Home", FormMethod.Post);
           {
               <input type="submit" value="Export To PDF" class=" btn" />
           }
       }
    </div>
    

 

Add an Export To PDF button in the index.cshtml in ASP.NET Core PDF

 

  1. Include the following namespaces and code snippet in the controller for converting HTML to PDF.
    // [C# Code]
    using Syncfusion.HtmlConverter;
    using Syncfusion.Pdf;
    using System.IO;
    

 

// [C# Code]
public ActionResult ExportToPDF()
{
     //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 Blink settings to HTML converter
     htmlConverter.ConverterSettings = settings;
 
     //Convert URL to PDF
     PdfDocument document = htmlConverter.Convert("https://www.syncfusion.com");
 
     MemoryStream stream = new MemoryStream();
 
     //Save and close the PDF document 
     document.Save(stream);
 
     return File(stream.ToArray(), System.Net.Mime.MediaTypeNames.Application.Pdf, "Sample.pdf");
}

 

  1. 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 Export to PDF option to convert the Syncfusion webpage to a PDF.

Click Export to PDF option to convert the Syncfusion webpage to a PDF in ASP.NET Core PDF

 

  1. By executing the program, you will get the PDF document as follows.

Output document in ASP.NET Core PDF

Deploy the container to Azure container instance:

 

  1. Create a publish target to deploy the docker image to Azure. 

Create a publish target to deploy the docker image to Azure in ASP.NET Core PDF

 

  1. Create Azure App Service with resource group, hosting plan, and container registry. Create Azure App Service with resource group, hosting plan, and container registry in ASP.NET Core PDF

 

  1. Publish the docker image to Azure container instance.

Publish the docker image to Azure container instance in ASP.NET Core PDF

 

  1. It will push the docker image to the Azure container registry and deploy it to the Azure container instance.

Repository downloading screenshot in ASP.NET Core PDF

 

  1. After successful deployment, it will open the Azure website in the browser.

Azure website in the browser in ASP.NET Core PDF

 

  1. Click Export to PDF option to convert the Google webpage to a PDF. You will get the PDF document as follows.

Output document in ASP.NET Core PDF

 

 

A complete work sample can be downloaded from BlinkLinuxDockerAzureSample.zip.

Take a moment to peruse the documentation where you can find converting HTML pages to PDF document along with respective customization options and features.

Click here to explore the rich set of the Syncfusion Essential PDF features.

An online sample link to convert HTML to PDF.

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied