We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to convert HTML to PDF in Azure App service Linux using .NET Core

Platform: ASP.NET Core |
Control: PDF |
Published Date: August 30, 2019 |
Last Revised Date: September 3, 2019

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 on Linux.

Steps to convert HTML to PDF in Azure App service on Linux:

  1. Create a new ASP.NET Core MVC application.

Create a New ASP.Net core MVC application.

Select a MVC project.

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

Install NET Core NuGet packages.

  1. Copy the QtBinariesLinux folder from the HtmltoPdfConverter NuGet package installed location to project folder, which contains the .csproj file.

Copy the QtBinariesLinux folder from package folder.

Paste the QtBinariesLinux folder to the project folder.

  1. Set Copy to output directory as Copy if newer for all the files in QtBinariesLinux (All files including inner folders and files).

Set copy to output directory as copy if newer to all QtBinariesLinux files.

  1. There are two ways to install the dependency packages to Azure server:
  1. Using SSH from Azure portal.
  2. By running the commands from C#.

5.1Using SSH command line:

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

Azure SSH window for install dependencies

  1. From the terminal window, you can install the dependency packages. Use the following single command to install all dependencies packages.
    apt-get update && apt-get -y install xvfb && apt-get -y install fontconfig && apt-get -y install libssl1.0-dev && apt-get -y install libx11-dev libx11-xcb-dev libxcb-icccm4-dev libxcb-image0-dev libxcb-keysyms1-dev libxcb-randr0-dev libxcb-render-util0-dev libxcb-render0-dev libxcb-shm0-dev libxcb-util0-dev libxcb-xfixes0-dev libxcb-xkb-dev libxcb1-dev libxfixes-dev libxrandr-dev libxrender-dev
     
    

 

5.2Running 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 followedto install dependencies packages.

Include dependencies packages shell to project location.

  1. Set Copy to output directory as Copy if newer to the dependenciesInstall.sh file.

Set copy to output directory as copy if newer

  1. Include the following code snippet to install the dependencies code in Configure method in startup.cs file.
    //Install the dependencies packages for HTML to PDF conversion in Linux
    string shellFilePath = System.IO.Path.Combine(env.ContentRootPath, "dependenciesInstall.sh");
    InstallDependecies(shellFilePath);
    

 

// [C# Code]
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();
}

 

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

 

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

 

// [C# Code]
//To get content root path of the project
private readonly IHostingEnvironment _hostingEnvironment;
public HomeController(IHostingEnvironment hostingEnvironment)
{
      _hostingEnvironment = hostingEnvironment;
} 
//To Export HTML to PDF
public IActionResult ExportToPDF()
{
Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "Development");
//Initialize HTML to PDF converter 
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
 
WebKitConverterSettings settings = new WebKitConverterSettings();
 
//Set WebKit path
settings.WebKitPath = Path.Combine(_hostingEnvironment.ContentRootPath, "QtBinariesLinux");
 
//Assign WebKit settings to HTML converter
htmlConverter.ConverterSettings = settings;
 
//Convert HTML string to PDF
PdfDocument document = htmlConverter.Convert("http://www.google.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 = "Output.pdf";
 
//Creates a FileContentResult object by using the file contents, content type, and file name
return File(stream, contentType, fileName);

 

Refer to the following steps to publish as Azure App Linux:

  1. Right-click the project and select Publish.

Right click the project and select publish

  1. Create a new profile in publish target window.

Pick a App Service Linux as Publish target

  1. Create App service using Azure subscription and select a hosting plan.

Create a Linux App service with new resource.

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

Configure Hosting plan

  1. After creating a profile, click the publish button.

Click Publish button.

  1. Now, the published webpage will open in the browser. Click Export to PDF to convert the Google webpage to a PDF.Output document image

A complete work sample for converting an HTML to PDF in Azure App service on Linux can be downloaded from AzureAppLinux_CoreSample.zip.

Take a moment to peruse the documentation, where you will find other WebKitBlink, and IE rendering engines supported by Syncfusion .NET PDF library for converting HTML pages to PDF document along with respective customization options and features.

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

An online sample link to convert HTML to PDF.

HTML to PDF conversion in Linux docker container

HTML to PDF conversion in Azure Function

HTML to PDF conversion in ASP .Net Core Linux

HTML to PDF conversion in Azure App Service

 

2X faster development

The ultimate ASP.NET Core UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile