How to use custom fonts for PDF creation in Azure function
The Syncfusion Essential PDF is a feature-rich and high performance .NET Core PDF library used to create, read, and edit PDF documents programmatically without Adobe dependencies. Using this library, you can use custom fonts for PDF creation in Azure functions.
Steps to create PDF with custom fonts in Azure functions programmatically:
- Create a new Azure function project.
- Select framework Azure Functions v1 (.NET Core) and select HTTP trigger as follows.
- Install the Syncfusion.Pdf.Net.Core NuGet package as a reference to your .NET Core project from the NuGet.org.
- Create a folder and copy the required font files and include the files to the project.
- Set the copy to output directory property to Copy if newer to all the font files.
- Refer here to add custom fonts in Azure Blob storage account and get the uploaded blob URL. In this example, you can get the font stream from Blob URL using the GetStreamFromUrl method.
C#
private static Stream GetStreamFromUrl(string url) { byte[] fontData = null; using (var web = new System.Net.WebClient()) fontData = web.DownloadData(url); return new MemoryStream(fontData); }
- Include the following namespaces in Function1.cs file.
C#
using Syncfusion.Pdf; using Syncfusion.Pdf.Graphics; using System.Net.Http.Headers; using System.Net; using System.Net.Http; using Microsoft.Azure.WebJobs;
- Add the following code snippet in Function1 to use custom font for PDF creation in Azure functions.
C#
//Creating the PDF document PdfDocument document = new PdfDocument(); //Adding the page PdfPage page = document.Pages.Add(); //Read font stream from blob to create a PdfFont Stream fontStreamFromBlob = GetStreamFromUrl("https://pdfcustomfontsample20190.blob.core.windows.net/pdfcustomfontcontainer/Pacifico.ttf"); //Create PdfFont from the stream got from blob PdfFont fontBlob = new PdfTrueTypeFont(fontStreamFromBlob, 24); //Drawing the Unicode string page.Graphics.DrawString("Hello PDF Sample - using font from Blob", fontBlob, PdfBrushes.Black, 50, 50); //Read the font stream from physical path from azure function app directory byte[] data = File.ReadAllBytes(Path.Combine(executionContext.FunctionAppDirectory, "Fonts", "Pacifico.ttf")); Stream fontStream = new MemoryStream(data); //Loading the custom TTF font PdfFont font = new PdfTrueTypeFont(fontStream, 24); //Drawing the Unicode string page.Graphics.DrawString("Hello PDF Sample - using font from Path", font, PdfBrushes.Black, 50, 100); MemoryStream ms = new MemoryStream(); document.Save(ms); ms.Position = 0; HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK); response.Content = new ByteArrayContent(ms.ToArray()); response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = "Sample.pdf" }; response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf"); return response;
- Right-click the project and select Publish. Then, create a new profile in the Publish Window.
- Create App service using Azure subscription.
- After creating the profile, click the Publish button.
- Now, go to Azure portal and select the Functions Apps. After running the service, click Get function URL > Copy. Paste the same in the new browser tab. You will get the output PDF document as follows.
A complete working sample can be downloaded from PDFCustomFont_Sample.zip.
Take a moment to peruse the documentation, where you will find other options like drawing right-to-left text and multicolumn text, consuming TrueType fonts, Standards fonts, and CJK fonts. Also, the features like PDF form filling, extract text or images from PDF, and protect PDF documents with code examples.
Click here to explore the rich set of Syncfusion Essential PDF features.
An online sample link to generate Hello world PDF document.
See Also:
Create a PDF file in ASP.NET Core
Create a PDF file in ASP.NET MVC
Create a PDF file in Windows Forms
Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.