Convert PPTX to PDF in Azure Functions (Flex Consumption)
29 Jun 202611 minutes to read
Syncfusion® PowerPoint is a .NET Core PowerPoint library used to create, read, edit and convert PowerPoint documents programmatically without Microsoft PowerPoint or interop dependencies. Using this library, you can convert a PowerPoint Presentation to PDF in Azure Functions deployed on Flex (Consumption) plan.
Steps to convert a PowerPoint Presentation to PDF in Azure Functions (Flex Consumption)
Step 1: Create a new Azure Functions project.

Step 2: Create a project name and select the location.

Step 3: Select function worker as .NET 8.0 (Long Term Support) (isolated worker) and target Flex/Consumption hosting suitable for isolated worker.

Step 4: Install the Syncfusion.PresentationRenderer.Net.Core and SkiaSharp.NativeAssets.Linux.NoDependencies v3.119.1 NuGet packages as references to your project from NuGet.org.


NOTE
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or from the NuGet feed, you also have to add “Syncfusion.Licensing” assembly reference and include a license key in your projects. Please refer to this link to know about registering Syncfusion® license key in your application to use our components.
Step 5: Include the following namespaces in the Function1.cs file.
using Syncfusion.Pdf;
using Syncfusion.Presentation;
using Syncfusion.PresentationRenderer;Step 6: Add the following code snippet in Run method of Function1 class to perform PowerPoint Presentation to PDF conversion in Azure Functions and return the resultant PDF to client end.
public async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequest req)
{
try
{
// Create a memory stream to hold the incoming request body (PowerPoint Presentation bytes)
await using MemoryStream inputStream = new MemoryStream();
// Copy the request body into the memory stream
await req.Body.CopyToAsync(inputStream);
// Check if the stream is empty (no file content received)
if (inputStream.Length == 0)
return new BadRequestObjectResult("No file content received in request body.");
// Reset stream position to the beginning for reading
inputStream.Position = 0;
// Load the PowerPoint Presentation from the stream
using IPresentation pptxDoc = Presentation.Open(inputStream);
// Attach font substitution handler to manage missing fonts
pptxDoc.FontSettings.SubstituteFont += FontSettings_SubstituteFont;
// Initialize the PresentationRenderer to perform PDF conversion.
PdfDocument pdfDocument = PresentationToPdfConverter.Convert(pptxDoc);
// Create a memory stream to hold the PDF output
await using MemoryStream outputStream = new MemoryStream();
// Save the PDF into the output stream
pdfDocument.Save(outputStream);
// Close the PDF document and release resources
pdfDocument.Close(true);
// Reset stream position to the beginning for reading
outputStream.Position = 0;
// Convert the PDF stream to a byte array
var pdfBytes = outputStream.ToArray();
// Create a file result to return the PDF as a downloadable file
var fileResult = new FileContentResult(pdfBytes, "application/pdf")
{
FileDownloadName = "converted.pdf"
};
// Return the PDF file result to the client
return fileResult;
}
catch (Exception ex)
{
// Log the error with details for troubleshooting
_logger.LogError(ex, "Error converting PPTX to PDF.");
// Prepare error message including exception details
var msg = $"Exception: {ex.Message}\n\n{ex}";
// Return a 500 Internal Server Error response with the message
return new ContentResult { StatusCode = 500, Content = msg, ContentType = "text/plain; charset=utf-8" };
}
}
/// <summary>
/// Event handler for font substitution during PDF conversion
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
private void FontSettings_SubstituteFont(object sender, SubstituteFontEventArgs args)
{
// Define the path to the Fonts folder in the application base directory
string fontsFolder = Path.Combine(AppContext.BaseDirectory, "Fonts");
// If the original font is Calibri, substitute with calibri-regular.ttf
if (args.OriginalFontName == "Calibri")
{
args.AlternateFontStream = File.OpenRead(Path.Combine(fontsFolder, "calibri-regular.ttf"));
}
// Otherwise, substitute with Times New Roman
else
{
args.AlternateFontStream = File.OpenRead(Path.Combine(fontsFolder, "Times New Roman.ttf"));
}
}Step 7: Right click the project and select Publish. Then, create a new profile in the Publish Window.

Step 8: Select the target as Azure and click Next button.

Step 9: Select the specific target as Azure Function App and click Next button.

Step 10: Select the Create new button.

Step 11: Click Create button.

Step 12: After creating app service then click Finish button.

Step 13: Click the Publish button.

Step 14: Publish has been succeed.

Step 15: Now, go to Azure portal and select the App Services. After running the service, click Get function URL by copying it. Then, paste it in the below client sample (which will request the Azure Functions, to perform PowerPoint Presentation to PDF conversion using the template PowerPoint document). You will get the output PDF as follows.

Steps to post the request to Azure Functions
Step 1: Create a console application to request the Azure Functions API.
Step 2: Add the following code snippet into Main method to post the request to Azure Functions with template PowerPoint document and get the resultant PDF.
static async Task Main()
{
Console.Write("Please enter your Azure Functions URL : ");
// Read the URL entered by the user and trim whitespace
string url = Console.ReadLine()?.Trim();
// If no URL was entered, exit the program
if (string.IsNullOrEmpty(url)) return;
// Create a new HttpClient instance for sending requests
using var http = new HttpClient();
// Read all bytes from the input PowerPoint Presentation file
var bytes = await File.ReadAllBytesAsync(@"Data/Input.pptx");
// Create HTTP content from the document bytes
using var content = new ByteArrayContent(bytes);
// Set the content type header to application/octet-stream (binary data)
content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");
// Send a POST request to the Azure Function with the document content
using var res = await http.PostAsync(url, content);
// Read the response content as a byte array
var resBytes = await res.Content.ReadAsByteArrayAsync();
// Get the media type (e.g., application/pdf or text/plain) from the response headers
string mediaType = res.Content.Headers.ContentType?.MediaType ?? string.Empty;
string outFile = mediaType.Contains("pdf", StringComparison.OrdinalIgnoreCase)
? Path.GetFullPath(@"../../../Output/Output.pdf")
: Path.GetFullPath(@"../../../Output/function-error.txt");
// Write the response bytes to the chosen output file
await File.WriteAllBytesAsync(outFile, resBytes);
Console.WriteLine($"Saved: {outFile} ");
}From GitHub, you can download the console application and Azure Functions Flex Consumption.
Looking for the full .NET PowerPoint Library component overview, features, pricing, and documentation? Visit the .NET PowerPoint Library page.
An online sample link to convert PowerPoint Presentation to PDF in ASP.NET Core.