Articles in this section
Category / Section

How to Convert Html to ASP.NET Core PDF in Azure App Service Linux?

9 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 HTML to PDF in Azure App Service on Linux using .NET Core PDF.

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

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

Create new core MVC project.

Select NET Core 3.1 target framework

  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.

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

3.1. Using SSH command line:

  1. After publishing the web application, log in 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.

Install dependencies using SSH command line.

  1. From the terminal window, you can install the dependency packages. Use the following single command to install all dependency packages:
    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

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 dependency packages.

Install dependencies using shell file.

  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.

  1. Include the following code snippet to install the dependencies code in Configure method in startup.cs file.
    // Install the dependency packages for HTML to PDF conversion in Linux
    string shellFilePath = System.IO.Path.Combine(env.ContentRootPath, "dependenciesInstall.sh");
    InstallDependencies(shellFilePath);
// [C# Code]
private void InstallDependencies(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 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 = "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 the publish target window.

Create a new profile in publish target window.

  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.

Publish the project

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

Output document

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

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

 

Conclusion

I hope you enjoyed learning about how to convert HTML to PDF in Azure App service Linux using .NET Core.

You can refer to our .NET Core PDF feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our .NET Core PDF examples 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 forums 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
Please  to leave a comment
Access denied
Access denied