Articles in this section
Category / Section

How to convert HTML to PDF in Azure App service Linux with Blink rendering engine?

7 mins read

The Syncfusion® HTML to PDF converter is a .NET Core 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 HTML to PDF in the Azure App Service on Linux.

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

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

Create ASP.NET Core MVC application 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 in ASP.NET Core PDF

 

  1. Install the below pre-requisites for BlinkBinaries using the shell script with C#.
    Note:

    We can install these pre-requisites using the SSH option in the Azure portal and avoid the below steps.

 

  1. Below are the commands to install the pre-requisites in the Azure App Service Linux.

    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

 

  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.

Install dependencies using shell file in ASP.NET Core PDF

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

Set Copy to output directory as Copy if newer to the shell file in ASP.NET Core PDF

  1. Include the following code snippet to install the dependencies code in the Configure method in the startup.cs file.
    //[C# code]
    //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.
    <div class="btn">
    @{ Html.BeginForm("ExportToPDF", "Home", FormMethod.Post);
       {
           <input type="submit" value="Export To PDF" class=" btn" />
       }
    }
    </div>
    
  1. Include the following namespaces and code snippets in the controller for converting HTML to PDF.
    // [C# Code]
    using Syncfusion.HtmlConverter;
    using Syncfusion.Pdf;
    using System.IO;
    

 

// [C# Code]
//To Export HTML to PDF
public IActionResult ExportToPDF()
{
Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "Development"); 
 
//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 Converter 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 = "Sample.pdf";
 
//Creates a FileContentResult object by using the file contents, content type, and file name
return File(stream, contentType, fileName);
}
  1. Build and publish the application to the Azure App Service Linux.

Build and publish the application to the Azure App Service Linux in ASP.NET Core PDF

Publish the project in ASP.NET Core PDF

  1. Now, the published webpage will open in the browser. Click Export to PDF to convert the Syncfusion webpage to a PDF.

Output document in ASP.NET Core PDF

 

A complete work sample for converting HTML to PDF in the Azure App service on Linux with the Blink rendering engine can be downloaded from AzureAppLinux_CoreSample.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 Syncfusion Essential® PDF features.

 

Conclusion

I hope you enjoyed learning about how to convert HTML to PDF in Azure App service Linux with Blink rendering engine.

You can refer to our PDF feature tour page to know about its other groundbreaking feature representations. You can also explore our documentation to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

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