Convert HTML to PDF file in Azure Functions on Windows

15 Mar 20246 minutes to read

As the Azure Windows platform is a Sandboxed environment, the default HTML rendering engine Blink used in our HTML to PDF conversion is incompatible due to GDI Limitations. For converting HTML to PDF in Azure Functions on Windows you can use the following approach that fit your requirement,

  • Legacy WebKit - this open-source library comes under LGPL license. It has some known rendering issues and limitations, and some of the advanced Bootstrap CSS styles are not supported.

Steps to convert HTML to PDF file in Azure Functions on Windows using legacy WebKit

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

Step 2: Create a project name and select the location.
Project naming

Step 3: Select the function worker as .NET 6.0(Long-term support), and the selected HTTP triggers as follows.
Convert HTMLToPDF Azure Functions Step3

Step 4: Install the Syncfusion.HtmlToPdfConverter.QtWebKit.Net.Core NuGet package as a reference to your project using Package Manager Console.
Convert HTMLToPDF Azure Functions Step4


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

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

using Syncfusion.HtmlConverter;
using Syncfusion.Pdf;

Step 6: Add the following code example in the Run method of the Function1 class to convert HTML to PDF document in Azure Functions and return the resultant PDF document.

public static async Task<HttpResponseMessage> Run(
    [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
    ILogger log, ExecutionContext context)
    MemoryStream ms = new MemoryStream();
    try {
        //Initialize HTML to PDF converter.
        HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter(HtmlRenderingEngine.WebKit);
        WebKitConverterSettings settings = new WebKitConverterSettings();
        //Set WebKit path.
        settings.WebKitPath = Path.Combine(context.FunctionAppDirectory, "bin/runtimes/win-x64/native");            
        //Assign WebKit settings to HTML converter.
        htmlConverter.ConverterSettings = settings;
        //Convert URL to PDF.
        PdfDocument document = htmlConverter.Convert("");

        //Save and close the PDF document.  
        ms.Position = 0;

        catch (Exception ex) {
            //Create a new PDF document.
            PdfDocument document = new PdfDocument();
            //Add a page to the document.
            PdfPage page = document.Pages.Add();
            //Create PDF graphics for the page.
            PdfGraphics graphics = page.Graphics;

            //Set the standard font.
            PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 5);
            //Draw the text.
            graphics.DrawString(ex.ToString(), font, PdfBrushes.Black, new Syncfusion.Drawing.PointF(0, 0));

            //Creating the stream object.
            ms = new MemoryStream();
            //Save the document into memory stream.
            //Close the document.
            ms.Position = 0;

        HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
        response.Content = new ByteArrayContent(ms.ToArray());
        response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") {
            FileName = "Sample.pdf"
        response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/pdf");
        return response;

Step 7: Copy the OpenSSL assemblies and paste them into the runtimes/win-x64/native folder containing the HTML_to_PDF_Azure_Functions.csproj file.


The OpenSSL libraries can be installed by downloading their setup from this link.

Right-click the project and select the publish option

Step 8: Set Copy if newer for all the OpenSSL assemblies.
Right-click the project and select the publish option

Step 9: Right-click the project and select Publish. Then, create a new profile in the Publish Window.
Create a new profile in the Publish Window

Step 10: Select the target as Azure and click Next.
Select the target as Azure

Step 11: Select the Azure Function App (Windows) and click Next.
Select Azure function app

Step 12: Select the Create new.
Configure Hosting Plan

Step 13: Click Create.
Browser will open after publish

Step 14: After creating the function app service, click Finish.
Creating app service

Step 15: Click Publish.
Click the Publish button

Step 16: Now, Publish has succeeded.
Publish has been succeeded

Step 17: Now, go to the Azure portal and select 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 a new browser tab. You will get a PDF document as follows.
Output document

You can download a complete working sample from GitHub.