We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to convert HTML to PDF in Azure Functions 2.0

Platform: ASP.NET Core |
Control: PDF |
Published Date: March 27, 2019 |
Last Revised Date: May 4, 2021

Syncfusion HTML to PDF for .NET Core 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 2.0.

In this tutorial, we will illustrate the conversion of HTML to PDF using C# with advanced WebKit 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 2.0

  1. Create an Azure Functions projects.

Create Azure function project

  1. Select framework to Azure Functions V2 (.NET Core) and select HTTP triggers as follows.

Select Http trigger

  1. Install the Syncfusion.HtmlToPdfConverter.QtWebKit.Net.Core NuGet package as a reference to the project.

install latest QtWebkit Net core

  1. Copy the QtBinariesWindows folder from the NuGet package installed location and paste it into the application folder, which contains HTMLtoPDFV20.csproj

copy Qtbinaries from nuget location

 

pasted the binaries folder inside the project location

  1. Set Copy to Output Directory property to Copy if newer to all the QtWebKit binaries.

Copy if newer to QtBinaries

  1. Include the following namespace in Functions1.cs file to convert the HTML to PDF using C#.

C#

using Syncfusion.HtmlConverter;
using Syncfusion.Pdf;

 

  1. Add the following code snippet in Function1 class to convert HTML to PDF in Azure Functions V2.
    string name = req.Query["url"];
     
    //Initialize HTML to PDF converter  
    HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
    WebKitConverterSettings settings = new WebKitConverterSettings();
     
    //Set WebKit path
    settings.WebKitPath = Path.Combine(executionContext.FunctionAppDirectory, "QtBinariesWindows");
    //Assign WebKit settings to HTML converter 
    htmlConverter.ConverterSettings = settings;
     
    //Convert URL to PDF 
    PdfDocument document = htmlConverter.Convert(name);
    MemoryStream ms = new MemoryStream();
    //Save the PDF document  
    document.Save(ms);
    ms.Position = 0;
     
    HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
    response.Content = new ByteArrayContent(ms.ToArray());
    response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
    {
        FileName = "HTMLToPDFAzure.pdf"
    };
    response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/pdf");
     
    return response;
    

 

  1. Right click the project and select publish. Create a new profile in the publish window.

 

When creating a publish target, please unselect the Run from package file option. So that, you can avoid the conversion failure due to the permission restrictions.

If you run from package file option, then the function permission becomes read only. But, the WebKit converter requires read/write/execute permission to perform the conversion. Refer to the following screenshot,

 

Creating a new profile. Unselect run from package file to avoid conversion failure.

  1. Create App service using Azure subscription and select a hosting plan.

Create App service

  1. HTML to PDF conversion will work from basic hosting plan (B1). So, select the hosing plan to B1 or greater and publish. HTML to PDF conversion will not work if the hosting plan is Consumption.

Configure Hosting plan

  1. Once the profile is created, click the publish button.

Publish

  1. Now, go to Azure portal and select the Functions Apps. After running the service, click the Get function URL -> Copy. Paste the same in the browser. In this example, need to pass the webpage URL in the query string, which needs to be converted to PDF.

(https://functionapp20190702110455.azurewebsites.net/api/Function1?url=http://www.google.com&code=***********************)

 

output pdf document image

A complete working sample can be downloaded from HTMLToPDF_AzureFunctions2.0

Convert secured or https sites to PDF in Azure Functions 2.0

When converting HTTPS sites, empty PDF may generate due to missing of the OPENSSL assemblies in Azure website. To convert HTTPS sites, the converter requires OPENSSL assemblies. By default, some Azure websites do not have the OPENSSL assemblies. So, these assemblies should be added to the website explicitly.

You can get the OPENSSL assemblies from here.

  1. Copy the OPENSSL assemblies into the QtBinariesWindows folder. Refer below screenshot,

copy Openssl assemblies into binaries folder

  1. Then set the copy to output directory as “Copy if newer” for all the files under QtBinariesWindows folder.

 

 

2X faster development

The ultimate ASP.NET Core UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment
Comments
Miguel Goicochea
Jul 04, 2019

Hello I tried following this example and while it works well locally (the report takes 3 minutes to generate). However, when I publish to Azure I get a timeout after the function has been running for less than 4 minutes (3min 50s). The azure function is using a dedicated app plan and I have had no issues when running other products I have been evaluating for a longer period, so I am not sure if I am missing any settings on the converter.

Reply
Gowthamraj Kumar [Syncfusion]
Jul 04, 2019

Hi Miguel,

The converter may throws the exception under Consumption/Free/Shared hosting plans on Azure. Please make sure you are not using the mentioned hosting plans to publish on Azure environment. Due to the access restrictions and limitation of these hosting plan, it prevents the loading of the browser process. So, the conversion will be failed or timeout in the Consumption/Free/Shared hosting plans on Azure environment. Other hosting plans does not have the restriction for loading the browser process.

If still you are facing the same error, kindly contact us through our Direct Trac

Regards,

Gowthamraj K

Underwood Studio
May 01, 2021

The OPENSSL assemblies link above doesn't work. It just tosses you to an "Access Denied" error page on S3. Can that link be updated?

Reply
Gowthamraj Kumar [Syncfusion]
May 04, 2021

Hi Underwood,

Sorry for the inconvenience caused.

We have updated the proper OPENSSL assemblies link in this KB article. Now you can download the assemblies from below link,

OPENSSL: https://www.syncfusion.com/downloads/support/directtrac/general/ze/OPENSSL-1821714558

Please refer the below link for more details about OPENSSL,

UG: https://help.syncfusion.com/file-formats/pdf/convert-html-to-pdf/webkit#openssl

Please let us know if you need any further assistance with this.

Regards,

Gowthamraj K

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile