Articles in this section
Category / Section

How to use custom fonts for PDF creation in Azure function

3 mins read

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:

  1. Create a new Azure function project. Create Azure Project
  2. Select framework Azure Functions v1 (.NET Core) and select HTTP trigger as follows. Select Http trigger
  3. Install the Syncfusion.Pdf.Net.Core NuGet package as a reference to your .NET Core project from the NuGet.org. Installed Syncfusion.Pdf.Net.Core NuGet package.
  4. Create a folder and copy the required font files and include the files to the project.

Copy Fonts folder in the project location

Include font folder

  1. Set the copy to output directory property to Copy if newer to all the font files.

copy if newer to font file

  1. 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);
        }

 

  1. 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;

 

  1. 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;

 

  1. Right-click the project and select Publish. Then, create a new profile in the Publish Window.

Pick a Publish target window

  1. Create App service using Azure subscription.

Create App service

  1. After creating the profile, click the Publish button.

 Publish

  1. 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.

Image of Output document

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 Xamarin

Create a PDF file in ASP.NET Core

Create a PDF file in ASP.NET MVC

Create a PDF file in Windows Forms

 

Note:

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.

 

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied