Articles in this section
Category / Section

How to create a PDF document in AWS Lambda

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 create a PDF document in AWS Lambda functions.

Setting up the AWS Toolkit for Visual Studio:

  1. You can create an AWS account by referring to this link.
  2. Download and install the AWS Toolkit for Visual Studio, you can download the AWS toolkit from this link. The Toolkit can be installed from Tools/Extension and updates options in Visual Studio.

Refer to the following steps to create a PDF document in AWS:

  1. Create an AWS Lambda function to create a PDF document and publish it to AWS.
  2. Invoke the AWS Lambda function in your main application using AWS SDKs.

Steps to create a PDF document in AWS Lambda function:

  1. Create a new AWS Lambda project as follows.Create AWS lambda project
  2. Select Blueprint as Empty Function and click Finish. Select Blank function as Blueprint
  3. Install the Syncfusion.Pdf.Net.Core NuGet package as a reference to your AWS lambda project from NuGet.org. Install NuGet packages
  4. Create a folder and copy the required data files and include the files to the project.

create and include Data folder

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

Set copy if newer to image file

  1. Include the following namespaces in Function.cs file.
    using Syncfusion.Pdf;
    using Syncfusion.Pdf.Graphics;
    using Syncfusion.Drawing;
    using System.IO;
    

 

  1. Add the following code snippet in Function.cs to create a PDF document.
    //Create a new PDF document
    PdfDocument document = new PdfDocument();
                
    //Add a page to the document
    PdfPage page = document.Pages.Add();
                
    //Create PDF graphics for the page
    PdfGraphics graphics = page.Graphics;
                
    //Set the standard font
    PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20);
                
    //Draw the text
    graphics.DrawString("Hello World!!!", font, PdfBrushes.Black, new PointF(0, 0));
                
    string imagePath = Path.GetFullPath(@"Data\logo.png");
     
    //Load the image from the disk
    FileStream imageStream = new FileStream(imagePath, FileMode.Open, FileAccess.Read);
     
    PdfBitmap image = new PdfBitmap(imageStream);
     
    //Draw the image
    graphics.DrawImage(image, 30, 30, 100, 25);
     
    //Save the document into stream
    MemoryStream stream = new MemoryStream();
                
    //Save the PDF document  
    document.Save(stream);
    document.Close();
     
    return Convert.ToBase64String(stream.ToArray());
    
  1. Right-click the project and select Publish to AWS Lambda. Publish to AWS Lambda
  2. 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 profile and name

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

Upload a lambda function

  1. After deploying the application, you can see the published Lambda function in AWS console.

AWS lambda function View

Refer to the following steps to invoke the AWS Lambda function from console application:

  1. Create a new console project.

Create a Console Application

  1. Install the AWSSDK.Core, AWSSDK.Lambda and Newtonsoft.Json package as a reference to your main application from the NuGet.org.

Install AWSSDK packages

Install AWSSDK lambda packages

Install Newtonsoft packages

  1. Include the following namespaces in Program.cs file.
    using Amazon;
    using Amazon.Lambda;
    using Amazon.Lambda.Model;
    using Newtonsoft.Json;
    
  1. Add the following code snippet in Program class 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.USEast1);
     
    //Create new InvokeRequest with the published function name
    InvokeRequest invoke = new InvokeRequest
    {
           FunctionName = "Mylambda",
           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 PDF document
    byte[] bytes = Convert.FromBase64String(responseText.ToString());
    FileStream fileStream = new FileStream("Sample.pdf", FileMode.Create);
    BinaryWriter writer = new BinaryWriter(fileStream);
    writer.Write(bytes, 0, bytes.Length);
    writer.Close();
    System.Diagnostics.Process.Start("Sample.pdf");
    

 

  1. By executing the program, you will get the PDF document as follows.

Output image

The samples of AWS Lambda and console applications are attached in this article for your reference. Find the samples from the following zip files.

AWS Lambda: AWSLambda.zip.

Console sample: ConsoleApp.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, Standard 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 MVC

Create a PDF file in WPF

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