Convert PowerPoint Presentation to Image in AWS Lambda

2 Dec 20247 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 image in AWS Lambda.

Steps to convert PowerPoint Presentation to Image in AWS Lambda

Step 1: Create a new AWS Lambda project as follows.
AWS Lambda project

Step 2: Select Blueprint as Empty Function and click Finish.
Select Blueprint as Empty Function

Step 3: Install the following Nuget packages in your application from Nuget.org.

Install Syncfusion.PresentationRenderer.Net.Core Nuget Package

Install SkiaSharp.NativeAssets.Linux.NoDependencies v2.88.8 Nuget Package

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 4: Create a folder and copy the required data files and include the files to the project.
Create a folder

Step 5: Set the copy to output directory to Copy if newer to all the data files.
Property change for data files

Step 6: Add the following environment variable in the aws-lambda-tools-defaults.json file to specify the library search paths for the AWS Lambda function. This configuration sets the LD_LIBRARY_PATH, allowing the application to locate the required native libraries at runtime.

"environment-variables": "\"LD_LIBRARY_PATH\"=\"/var/task:/tmp:/lib64:/usr/lib64\""

Step 7: Defining library paths in an AWS Lambda project enables the application to locate the necessary native libraries at runtime. This is essential for ensuring that the application functions correctly across different environments. The following code snippet illustrates how to define these paths.

//Path to the original library file.
string originalLibraryPath = "/lib64/libdl.so.2";

//Path to the symbolic link where the library will be copied.
string symlinkLibraryPath = "/tmp/libdl.so";

Step 8: Include the following namespaces in Function.cs file.

using Syncfusion.Presentation;
using Syncfusion.PresentationRenderer;
using Syncfusion.Drawing;

step 9: Add the following code snippet in Function.cs to convert a PowerPoint Presentation to image.

/// <summary>
/// A simple function that takes a string and does a ToUpper
/// </summary>
/// <param name="input"></param>
/// <param name="context"></param>
/// <returns></returns>
public string FunctionHandler(string input, ILambdaContext context)
{
    //Path to the original library file.
    string originalLibraryPath = "/lib64/libdl.so.2";

    //Path to the symbolic link where the library will be copied.
    string symlinkLibraryPath = "/tmp/libdl.so";

    //Check if the original library file exists.
    if (File.Exists(originalLibraryPath))
    {
        //Copy the original library file to the symbolic link path, overwriting if it already exists.
        File.Copy(originalLibraryPath, symlinkLibraryPath, true);
    }

    string filePath = Path.GetFullPath(@"Data/Input.pptx");
    //Open the existing PowerPoint presentation with loaded stream.
    using (IPresentation pptxDoc = Presentation.Open(filePath))
    {
        //Hooks the font substitution event.
        pptxDoc.FontSettings.SubstituteFont += FontSettings_SubstituteFont;
        //Initialize the PresentationRenderer to perform image conversion.
        pptxDoc.PresentationRenderer = new PresentationRenderer();
        //Convert PowerPoint slide to image as stream.
        Stream stream = pptxDoc.Slides[0].ConvertToImage(ExportImageFormat.Jpeg);
        //Unhooks the font substitution event after converting to image file.
        pptxDoc.FontSettings.SubstituteFont -= FontSettings_SubstituteFont;
        //Reset the stream position.
        stream.Position = 0;
        //Create a memory stream to save the image.
        MemoryStream memoryStream = new MemoryStream();
        stream.CopyTo(memoryStream);
        return Convert.ToBase64String(memoryStream.ToArray());
    }
}

//Set the alternate font when a specified font is not installed in the production environment.
private void FontSettings_SubstituteFont(object sender, SubstituteFontEventArgs args)
{
    if (args.OriginalFontName == "Calibri" && args.FontStyle == FontStyle.Regular)
        args.AlternateFontStream = new FileStream(Path.GetFullPath(@"Data/calibri.ttf"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
    else
        args.AlternateFontStream = new FileStream(Path.GetFullPath(@"Data/times.ttf"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
}

Step 10: Right-click the project and select Publish to AWS Lambda.
Publish to AWS Lambda

Step 11: Create a new AWS profile in the Upload Lambda Function Window. After creating the profile, add a name for the Lambda function to publish. Then, click Next.
Upload Lambda Function

Step 12: In the Advanced Function Details window, specify the Role Name as based on AWS Managed policy. After selecting the role, click the Upload button to deploy your application.
Advance Function Details

Step 13: After deploying the application, you can see the published Lambda function in AWS console.
After deploying the application

Step 14: Edit Memory size and Timeout as maximum in General configuration of the AWS Lambda function.
AWS Lambda Function

Steps to post the request to AWS Lambda

Step 1: Create a new console project.
Create a console project

step 2: Install the following Nuget packages in your application from Nuget.org.

Step 3: Include the following namespaces in Program.cs file.

using Amazon;
using Amazon.Lambda;
using Amazon.Lambda.Model;
using Newtonsoft.Json;

Step 4: Add the following code snippet in Program.cs to invoke the published AWS Lambda function using the function name and access keys.

//Create a new AmazonLambdaClient
AmazonLambdaClient client = new AmazonLambdaClient("awsaccessKeyID", "awsSecreteAccessKey", RegionEndpoint.USEast2);
//Create new InvokeRequest with published function name.
InvokeRequest invoke = new InvokeRequest
{
    FunctionName = "MyNewFunction",
    InvocationType = InvocationType.RequestResponse,
    Payload = "\"Test\""
};
//Get the InvokeResponse from client InvokeRequest.
InvokeResponse response = client.Invoke(invoke);
//Read the response stream
var stream = new StreamReader(response.Payload);
JsonReader reader = new JsonTextReader(stream);
var serilizer = new JsonSerializer();
var responseText = serilizer.Deserialize(reader);
//Convert Base64String into image file.
byte[] bytes = Convert.FromBase64String(responseText.ToString());
FileStream fileStream = new FileStream("PPTXtoImage.Jpeg", FileMode.Create);
BinaryWriter writer = new BinaryWriter(fileStream);
writer.Write(bytes, 0, bytes.Length);
writer.Close();
System.Diagnostics.Process.Start("PPTXtoImage.Jpeg");

By executing the program, you will get the image as follows.

PPTX to Image in AWS Lambda

From GitHub, you can download the console application and AWS Lambda project.

Click here to explore the rich set of Syncfusion® PowerPoint Library (Presentation) features.

An online sample link to convert PowerPoint Presentation to image in ASP.NET Core.