How to create a PDF document in AWS Lambda
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:
- You can create an AWS account by referring to this link.
- 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:
- Create an AWS Lambda function to create a PDF document and publish it to AWS.
- Invoke the AWS Lambda function in your main application using AWS SDKs.
Steps to create a PDF document in AWS Lambda function:
- Create a new AWS Lambda project as follows.
- Select Blueprint as Empty Function and click Finish.
- Install the Syncfusion.Pdf.Net.Core NuGet package as a reference to your AWS lambda project from NuGet.org.
- Create a folder and copy the required data files and include the files to the project.
- Set the copy to output directory to Copy if newer to all the data files.
- Include the following namespaces in Function.cs file.
using Syncfusion.Pdf; using Syncfusion.Pdf.Graphics; using Syncfusion.Drawing; using System.IO;
- 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());
- Right-click the project and select Publish to AWS Lambda.
- 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.
- 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.
- After deploying the application, you can see the published Lambda function in AWS console.
Refer to the following steps to invoke the AWS Lambda function from console application:
- Create a new console project.
- Install the AWSSDK.Core, AWSSDK.Lambda and Newtonsoft.Json package as a reference to your main application from the NuGet.org.
- Include the following namespaces in Program.cs file.
using Amazon; using Amazon.Lambda; using Amazon.Lambda.Model; using Newtonsoft.Json;
- 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");
- By executing the program, you will get the PDF document as follows.
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 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.