Creating PDF reports using timely triggered Azure Functions V2

For any kind of system, reports are essential. It provides statistical analysis about the system which could be used to make managerial decisions about a business, help to attract investors and help to provide useful information about the system to the end users. In this blog post, I’m going to talk about how to create a PDF report in a specific time of the day using an Azure Function. For example, people who are handling e-commerce web sites would like to have a report about total product clicks at the end of each day. Sellers would like to have a report of their total sales at the end of each day etc.

Creating Azure Functions

First, we will create an Azure Function in visual studio. You can follow this link to learn how to create an Azure Function and publish it to Azure. When adding a new Azure Function to the Azure Function project, select Time Trigger as shown in the below diagram.

Issues in generating PDF reports in Azure environment

Before starting coding there are few challenges to overcome when you publish the Azure Function in Azure environment. All azure web apps (as well as Mobile App/Services, WebJobs and Functions) run in Azure sandbox which provide limited access to the resources of the deployed machine. The sandbox prevents an app from using most of the kernel’s graphics API, which many pdf generators use either directly or indirectly. You can read more about the Azure sandbox from here.

Solution to overcome the issue

The same document lists some pdf generators which are allowed by the sandbox environment when you have Basic or higher app service plan. So, if you are using Consumption plan, to succeed in creating PDF reports you must upgrade to basic or higher app service plan. One library which is listed directly or indirectly in the list is wkhtmltopdf. In our attempt we will be using a wrapper around wkhtmltopdf. I could not find a better wrapper implemented in .Net Core which provides our needs. Therefore, I used Tuespechkin library which is a .Net wrapper for wkhtmltopdf library which hides all the P/Invoke messiness. With the Tuespechkin Nuget, you have to install TuesPechkin.Wkhtmltox.AnyCPU Nuget which provides the wkhtmltox.dll for Tuespechkin which enables it to run without any hassle.

Dependency Injection in Azure function

The next problem you come across when you work with the Azure Functions is the dependency injection. Currently Azure Functions does not have built-in support for dependency injection. So, you can use the following approach to inject your dependencies.

This function will run on midnight of everyday and generate the PDF report. One thing to keep in mind is that by default Azure Functions run on Coordinated Universal Time (UTC). To run the Azure Function in specific time zone, add a new app setting named WEBSITE_TIME_ZONE and set the value as shown in the Microsoft Time Zone Index. By changing the cron expression (“0 0 0 * * *”) you can change the execution time of the Azure Function.