How to convert HTML to PDF in Azure App service Linux with Blink rendering engine?
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:
- Create a new ASP.NET Core MVC application.
- Install the Syncfusion.HtmlToPdfConverter.Net.Linux NuGet package as a reference to your .NET Core application from NuGet.org.
- 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.
- 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
- 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.
- Set Copy to output directory as Copy if newer to the dependenciesInstall.sh file.
- 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(); }
- 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>
- 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);
}
- Build and publish the application to the Azure App Service Linux.
- Now, the published webpage will open in the browser. Click Export to PDF to convert the Syncfusion webpage to a 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 forums, Direct-Trac, or feedback portal. We are always happy to assist you!