How to convert HTML to PDF in Azure Functions 4.0 with ASP.NET Core PDF?
Syncfusion® HTML to PDF converter for our ASP.NET Core PDF Library's feature tour page used to convert web pages, and HTML to PDF. Using this library, you can convert any HTML strings or URL or web pages to PDF in Azure Functions 4.0(NET 6).
In this tutorial, we will illustrate the conversion of HTML to PDF using C# with advanced Blink rendering engine. In addition, our HTML to PDF converter will work seamlessly in various platforms like Azure cloud or web apps, Amazon Web Service (AWS), Docker, WinForms, WPF, ASP.NET MVC, ASP.NET Core with Windows, Linux, and MacOS.
Steps to convert HTML to PDF in Azure Functions 4.0
- Create an Azure Functions projects.
- Select framework to Azure Functions V4 (.NET 6) and select HTTP triggers as follows.
- Install the Syncfusion.HtmlToPdfConverter.Net.Linux NuGet package as a reference to the project.
- Include the following namespace in Functions1.cs file to convert the HTML to PDF using C#.
using Syncfusion.HtmlConverter; using Syncfusion.Pdf; using System.Runtime.InteropServices;
C#
- Add the following code snippet in Function1 class to convert HTML to PDF in Azure Functions V4.
[FunctionName("Function1")] public static async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log, ExecutionContext executionContext) { string blinkBinariesPath = string.Empty; try { blinkBinariesPath = SetupBlinkBinaries(executionContext); } catch { throw new Exception("BlinkBinaries initialization failed"); } string url = req.Query["url"]; //Initialize the HTML to PDF converter with the Blink rendering engine. HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter(HtmlRenderingEngine.Blink); BlinkConverterSettings settings = new BlinkConverterSettings(); //Set command line arguments to run without sandbox. settings.CommandLineArguments.Add("--no-sandbox"); settings.CommandLineArguments.Add("--disable-setuid-sandbox"); settings.BlinkPath = blinkBinariesPath; //Assign Blink Converter settings to the HTML converter htmlConverter.ConverterSettings = settings; //Convert URL to PDF PdfDocument document = htmlConverter.Convert(url); MemoryStream ms = new MemoryStream(); //Save and close the PDF document document.Save(ms); document.Close(); ms.Position = 0; return new FileStreamResult(ms, "application/pdf"); }
- Add the following helper methods to copy and set permission to the BlinkBinariesLinux folder.
private static string SetupBlinkBinaries(ExecutionContext executionContext) { string blinkAppDir = Path.Combine(executionContext.FunctionAppDirectory, "bin/runtimes/linux/native"); string tempBlinkDir = Path.GetTempPath(); string chromePath = Path.Combine(tempBlinkDir, "chrome"); if (!File.Exists(chromePath)) { CopyFilesRecursively(blinkAppDir, tempBlinkDir); SetExecutablePermission(tempBlinkDir); } return tempBlinkDir; } private static void CopyFilesRecursively(string sourcePath, string targetPath) { //Create all the directories from the source to the destination path. foreach (string dirPath in Directory.GetDirectories(sourcePath, "*", SearchOption.AllDirectories)) { Directory.CreateDirectory(dirPath.Replace(sourcePath, targetPath)); } //Copy all the files from the source path to the destination path. foreach (string newPath in Directory.GetFiles(sourcePath, "*.*", SearchOption.AllDirectories)) { File.Copy(newPath, newPath.Replace(sourcePath, targetPath), true); } } [DllImport("libc", SetLastError = true, EntryPoint = "chmod")] internal static extern int Chmod(string path, FileAccessPermissions mode); private static void SetExecutablePermission(string tempBlinkDir) { FileAccessPermissions ExecutableFilePermissions = FileAccessPermissions.UserRead | FileAccessPermissions.UserWrite | FileAccessPermissions.UserExecute | FileAccessPermissions.GroupRead | FileAccessPermissions.GroupExecute | FileAccessPermissions.OtherRead | FileAccessPermissions.OtherExecute; string[] executableFiles = new string[] { "chrome", "chrome_sandbox" }; foreach (string executable in executableFiles) { var execPath = Path.Combine(tempBlinkDir, executable); if (File.Exists(execPath)) { var code = Function1.Chmod(execPath, ExecutableFilePermissions); if (code != 0) { throw new Exception("Chmod operation failed"); } } } }
- Include the below enum in the Function1.cs file.
[Flags] internal enum FileAccessPermissions : uint { OtherExecute = 1, OtherWrite = 2, OtherRead = 4, GroupExecute = 8, GroupWrite = 16, GroupRead = 32, UserExecute = 64, UserWrite = 128, UserRead = 256 }
Public to Azure Functions Linux:
- Right-click the project and select Publish. Then, create a new profile in the Publish Window. The Blink rendering engine will work in consumption plan. So, you can create the Azure Function App service with a consumption plan.
- After creating the profile, click the Publish button.
- Now, go to the Azure portal and select the App Services. After running the service, click Get function URL > Copy. Include the URL as a query string in the URL. Then, paste it into the new browser tab. You will get the PDF document as follows.
Example: https://htmltopdfazurefunctionblink.azurewebsites.net/api/Function1?code=aYCeymbJeHMGIwEEIuCIcNe….&url=https://www.syncfusion.com
A complete working sample can be downloaded from HtmlToPdf_AzureFunctions4.0.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.
I hope you enjoyed learning about how to convert html to pdf in Azure portal.
You can refer to our ASP.NET Core PDF Library's 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 ASP.NET Core PDF Library example to understand how to create and manipulate data.
For current customers, you can check out our Document processing libraries 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!