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
- Create an Azure Functions projects.
- Select framework to Azure Functions V2 (.NET Core) and select HTTP triggers as follows.
- Install the Syncfusion.HtmlToPdfConverter.QtWebKit.Net.Core NuGet package as a reference to the project.
- Copy the QtBinariesWindows folder from the NuGet package installed location and paste it into the application folder, which contains HTMLtoPDFV20.csproj
- Set Copy to Output Directory property to Copy if newer to all the QtWebKit binaries.
- Include the following namespace in Functions1.cs file to convert the HTML to PDF using C#.
- 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
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");
- 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,
- Create App service using Azure subscription and select a hosting plan.
- 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.
- Once the profile is created, click the publish button.
- 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.
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.
- Copy the OPENSSL assemblies into the QtBinariesWindows folder. Refer below screenshot,
- Then set the copy to output directory as “Copy if newer” for all the files under QtBinariesWindows folder.