Deployment to Azure Functions | |
SelectPdf runs in Azure Functions (version 4, .NET isolated worker) on Windows and on Linux, on a Dedicated (App Service) plan of Basic or above, or on an Elastic Premium plan.
Plan | SelectPdf |
|---|---|
Dedicated (App Service), Basic or above | supported, Windows and Linux |
Elastic Premium (EP1 and up) | supported, Windows and Linux |
Consumption (Windows) | not supported |
Flex Consumption (Linux) | not supported |
A Windows Consumption plan cannot run the engine: the deployment does not fit the disk space the plan gives an application, Run From Package mounts the engine where it cannot be loaded, and the plan's sandbox stops the engine even when it is copied elsewhere. Flex Consumption always runs the application from the mounted, read-only package.
The same packages as a web application: SelectPdf.Universal with SelectPdf.Universal.Native.win-x64 on Windows, or with SelectPdf.Universal.Native.linux-x64 and SelectPdf.Universal.Fonts on Linux.
1. Create a resource group, a storage account and a plan (--is-linux for Linux; --sku EP1 for Elastic Premium):
az login
az group create --name selectpdf-rg --location westeurope
az storage account create --resource-group selectpdf-rg \
--name selectpdfstorage --sku Standard_LRS
az functionapp plan create --resource-group selectpdf-rg \
--name selectpdf-fnplan --sku P1v3 --is-linux2. Create the function app on that plan, with Always On (a Dedicated plan option):
az functionapp create --resource-group selectpdf-rg \
--name selectpdf-fn --plan selectpdf-fnplan \
--storage-account selectpdfstorage --functions-version 4 \
--runtime dotnet-isolated --runtime-version 10 --os-type Linux
az functionapp config set --resource-group selectpdf-rg \
--name selectpdf-fn --always-on trueOn Windows use --os-type Windows and also pass --use-32bit-worker-process false to the second command.
3. Publish and deploy:
dotnet publish -c Release -o publish
cd publish && zip -r ../fn.zip . && cd ..
az functionapp deployment source config-zip --resource-group selectpdf-rg \
--name selectpdf-fn --src fn.zipA function app on a Dedicated plan does not use Run From Package, and the zip deployment does not turn it on, so the engine runs from a writable folder as in a web application. Do not set WEBSITE_RUN_FROM_PACKAGE (see Azure App Service).
A function that converts the page given in the query string:
[Function("Convert")] public static HttpResponseData Run( [HttpTrigger(AuthorizationLevel.Function, "get")] HttpRequestData req) { string url = HttpUtility.ParseQueryString(req.Url.Query)["url"]; HtmlToPdf converter = new HtmlToPdf(); PdfDocument doc = converter.ConvertUrl(url); byte[] pdf = doc.Save(); doc.Close(); HttpResponseData response = req.CreateResponse(HttpStatusCode.OK); response.Headers.Add("Content-Type", "application/pdf"); response.Body.Write(pdf, 0, pdf.Length); return response; }
The official samples include a complete Azure Functions project for Windows and for Linux (windows/Functions, linux/Functions).
Function apps run on the same machines as web applications, so the times of Azure App Service apply: on P1v3 a warm instance converts selectpdf.com in under 2 seconds, and Basic or Standard plans take several times longer.