Create PDF files on Azure
GemBox.Pdf is a standalone .NET component that's ideal for web applications because of its fast performance and thread safety when working with multiple PdfDocument
objects.
You can use GemBox.Pdf on various platforms, including Azure Functions and Azure App Services. To create an Azure function, search for "Azure Functions" in a new project dialog. To avoid any potential issues with GemBox.Pdf, we recommend using the newer versions of Azure Functions (v3 or above). The following is an Azure function project file: Note that saving to XPS and image formats (like PNG and JPG) currently works only on Azure Functions that target .NET Framework. The following example shows how to create a PDF document using GemBox.Pdf in an Azure Function. App Service is a fully managed platform for building, deploying and scaling web apps. GemBox.Pdf can be used from applications that run on an App Service. To build an ASP.NET Core application, check out our ASP.NET Core example. To publish an application to Azure App service you need to: 1. Pick Azure as the publish target. 2. Pick Azure App Service as a specific target. 3. Specify existing App Service or create a new one. You can use the full functionality of GemBox.Pdf on Azure Virtual Machine, Azure Cloud Services, and Azure Functions. But for App Services, there are few rendering limitations: These features currently have WPF dependencies which means they require a .NET Windows Desktop Runtime. However, we do have plans for providing cross-platform support for them in future releases.Azure Functions
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
<OutputType>Exe</OutputType>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="GemBox.Pdf" Version="*" />
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.23.1" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.2.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.18.1" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
</ItemGroup>
</Project>
using GemBox.Pdf;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using System.IO;
using System.Net;
using System.Threading.Tasks;
public class GemBoxFunction
{
[Function("GemBoxFunction")]
public async Task<HttpResponseData> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get")] HttpRequestData req)
{
// If using the Professional version, put your serial key below.
ComponentInfo.SetLicense("FREE-LIMITED-KEY");
using (var document = new PdfDocument())
{
// Add a first empty page.
document.Pages.Add();
// Add a second empty page.
document.Pages.Add();
var fileName = "Output.pdf";
using var stream = new MemoryStream();
document.Save(stream);
var bytes = stream.ToArray();
var response = req.CreateResponse(HttpStatusCode.OK);
response.Headers.Add("Content-Type", "application/pdf");
response.Headers.Add("Content-Disposition", "attachment; filename=" + fileName);
await response.Body.WriteAsync(bytes, 0, bytes.Length);
return response;
}
}
}
Imports GemBox.Pdf
Imports Microsoft.Azure.Functions.Worker
Imports Microsoft.Azure.Functions.Worker.Http
Imports System.IO
Imports System.Net
Imports System.Threading.Tasks
Public Class GemBoxFunction
<[Function]("GemBoxFunction")>
Public Async Function Run(<HttpTrigger(AuthorizationLevel.Anonymous, "get")> req As HttpRequestData) As Task(Of HttpResponseData)
' If using the Professional version, put your serial key below.
ComponentInfo.SetLicense("FREE-LIMITED-KEY")
Using document As New PdfDocument()
' Add a first empty page.
document.Pages.Add()
' Add a second empty page.
document.Pages.Add()
Dim fileName = "Output.pdf"
Using stream As New MemoryStream()
document.Save(stream)
Dim bytes = stream.ToArray()
Dim response = req.CreateResponse(HttpStatusCode.OK)
response.Headers.Add("Content-Type", "application/pdf")
response.Headers.Add("Content-Disposition", "attachment; filename=" & fileName)
Await response.Body.WriteAsync(bytes, 0, bytes.Length)
Return response
End Using
End Using
End Function
End Class
Azure App Services
Limitations on App Services
ConvertToImageSource
and ConvertToXpsDocument
methods.