Convert HTML to PDF in Azure Functions Linux

3 Jul 20249 minutes to read

The Syncfusion HTML to PDF converter is a .NET Core library for converting webpages, SVG, MHTML, and HTML to PDF using C#. 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 using C# with the Blink rendering engine in Azure Functions Linux.

NOTE

HTML to PDF converter is not supported with Azure App Service windows. We internally use Blink rendering engine for the conversion, it uses GDI calls for viewing and rendering the webpages. But Azure app service blocks GDI calls in the Azure website environment. As the Azure website does not have the elevated permission and enough rights, we can not launch the Chrome headless browser in the Azure app service windows (Azure website and Azure function).

Step 1: Create the Azure function project.
Convert HTMLToPDF Azure Functions Step1

Step 2: Select the Azure Functions type and .NET Core version.
Convert HTMLToPDF Azure Functions Step2

Step 3: Install the Syncfusion.HtmlToPdfConverter.Net.Linux NuGet package as a reference to your .NET Core application NuGet.org.
Convert HTMLToPDF Azure Functions Step3

NOTE

Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, you also have to add “Syncfusion.Licensing” assembly reference and include a license key in your projects. Please refer to this link to know about registering Syncfusion license key in your application to use our components.

Step 4: Include the following namespaces in Function1.cs file.

  • C#
  • using Syncfusion.HtmlConverter;
    using Syncfusion.Pdf;
    using System.Runtime.InteropServices;

    Step 5: Add the following code example in the Function1 class to convert HTML to PDF document using Convert method in HtmlToPdfConverter class. The Blink command line arguments based on the given CommandLineArguments property of BlinkConverterSettings class.

  • C#
  • [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 BlinkConverter 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");
        }

    Step 6: Add the following helper methods to copy and set permission to the BlinkBinariesLinux folder.

  • C#
  • 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");
                    }
                }
            }
        }

    Step 7: Include the below enum in the Function1.cs file.

  • C#
  • [Flags]
    internal enum FileAccessPermissions : uint
    {
        OtherExecute = 1,
        OtherWrite = 2,
        OtherRead = 4,
        GroupExecute = 8,
        GroupWrite = 16,
        GroupRead = 32,
        UserExecute = 64,
        UserWrite = 128,
        UserRead = 256
    }

    Publish to Azure Functions Linux

    Step 1: 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.
    Convert HTMLToPDF Azure Functions Step4

    Step 2: After creating the profile, click the Publish button.
    Convert HTMLToPDF Azure Functions Step5

    Step 3: 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.
    Convert HTMLToPDF Azure Functions Step6

    A complete working sample can be downloaded from Github

    Click here to explore the rich set of Syncfusion HTML to PDF converter library features.

    An online sample link to convert HTML to PDF document in ASP.NET Core.

    Click here to explore the rich set of Syncfusion HTML to PDF converter library features.

    An online sample link to convert HTML to PDF document in ASP.NET Core.