- Steps to convert HTML to PDF file in Azure Functions on Windows using CefSharp
- Steps to publish as Azure Function on Windows
Contact Support
Convert HTML to PDF file in Azure Functions on Windows
20 Jan 20257 minutes to read
As the Azure Windows platform is a Sandbox environment, the default HTML rendering engine Blink used in our HTML to PDF conversion is incompatible due to GDI Limitations. It is recommended that you use Azure functions in Linux For converting HTML to PDF in Azure Functions on Windows, you can use our CefSharp based HTML converter if is fit your requirement.
NOTE
CefSharp is an open-source library that comes under the BSD license.
Steps to convert HTML to PDF file in Azure Functions on Windows using CefSharp
Step 1: Create the Azure functions project.
Step 2: Create a project name and select the location.
Step 3: Select the function worker as .NET 8.0 isolated (Long-term support), and the selected HTTP triggers as follows.
NOTE
We have ensured the conversion in Azure functions isolated app and the conversion supports Azure functions isolated app only. The normal Azure Function app has a limitation of copying the runtime files at publish.
Step 4: Install the Syncfusion.HtmlToPdfConverter.Cef.Net.Windows NuGet package to reference your project using the nuget.org.
NOTE
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;
using Syncfusion.Pdf.Graphics;
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.
[Function("Function1")]
public IActionResult Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequest req)
{
string blinkBinariesPath = string.Empty;
MemoryStream ms = null;
try
{
//Initialize the HTML to PDF converter with the Blink rendering engine.
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter(HtmlRenderingEngine.Cef);
//Convert URL to PDF
PdfDocument document = htmlConverter.Convert("https://www.google.com/");
ms = new MemoryStream();
//Save and close the PDF document
document.Save(ms);
document.Close();
}
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, 20);
//Draw the text.
graphics.DrawString(ex.Message, font, PdfBrushes.Black, new Syncfusion.Drawing.PointF(0, 0));
//Creating the stream object.
ms = new MemoryStream();
//Save the document into memory stream.
document.Save(ms);
//Close the document.
document.Close(true);
}
ms.Position = 0;
return new FileStreamResult(ms, "application/pdf");
}
Step 7: Refer the steps to publish as Azure Function on Windows.
Step 8: After publish open the created web app service in the Azure portal. Go to Settings -> Configuration -> Platform settings and change the platform to 64-bit.
You can download a complete working sample from GitHub.
Steps to publish as Azure Function on Windows
Step 1: Right-click the project and select Publish. Then, create a new profile in the Publish Window.
Step 2: Select the target as Azure and click Next.
Step 3: Select the Azure Function App (Windows) and click Next.
Step 4: Select the Create new.
Step 5: Click Create.
Step 6: After creating the function app service, click Finish.
Step 7: Click deployment type.
Step 8: Click Close button.
Step 9: Click Publish.
Step 10: Now, Publish has succeeded.
Step 11: 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.