Articles in this section
Category / Section

How to convert HTML to PDF in AWS Lambda?

3 mins read

The Syncfusion HTML to PDF converter is a .NET library for converting webpages, SVG, MHTML, and HTML to PDF using C#. It is reliable and accurate. Using this library, you can convert HTML to PDF in AWS Lambda using Blink.

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 convert HTML to PDF in AWS Lambda:

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

Steps to convert HTML to PDF in AWS Lambda:

  1. Create a new AWS Lambda project as follows:Create a new AWS Lambda project.
  2. Create a project name and select location.Create a project name and select location.

 

  1. Select Blueprint as Empty Function and click Finish.

Select Blueprint as Empty Function and click Finish.

  1. Install the Syncfusion.HtmlToPdfConverter.Net.Aws NuGet package as a reference to your AWS lambda project from NuGet.org. Install latest Syncfusion HTML to PDF AWS NuGet Package as reference.
  2. Using the following namespaces in the Function.cs file.
    using Syncfusion.HtmlConverter;
    using Syncfusion.Pdf;
    using System.IO;
    

 

  1. Add the following code snippet in Function.cs to create a PDF document.
    //Initialize HTML to PDF converter with Blink rendering engine.
    HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
     
    //Convert URL to PDF.
    PdfDocument document = htmlConverter.Convert(input);
     
    //Save the document into stream.
    MemoryStream memoryStream = new MemoryStream();
     
    //Save and Close the PDFDocument.
    document.Save(memoryStream);
    document.Close(true);
     
    return Convert.ToBase64String(memoryStream.ToArray());
    

 

  1. Right-click the project and select Publish to AWS Lambda. Right-click the project and select 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.   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.     

 

  

  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. 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.
  2. After deploying the application, Sign in your AWS account and you can see the published Lambda function in AWS console.After deploying the application, Sign in your AWS account and you can see the published Lambda function in AWS console

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

  1. Create a new console project. Create a new console project.
  2. Create a project name and select folder path.Create a project name and select folder path.

 

  1. Install the AWSSDK.Core, AWSSDK.Lambda and Newtonsoft.Json package as a reference to your main application from the NuGet.org. Install the AWSSDK.Lambda package as a reference to your main application from the NuGet.org. Install the AWSSDK.Core package as a reference to your main application from the NuGet.org. Install the Newtonsoft.Json package as a reference to your main application from the NuGet.org.
  2. Include the following namespaces in Program.cs file.
    using Amazon;
    using Amazon.Lambda;
    using Amazon.Lambda.Model;
    using Newtonsoft.Json;
    using System.IO;
    

 

  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 = "AwsLambdaFunctionHtmlToPdfConversion",
      InvocationType = InvocationType.RequestResponse,
      Payload = "\" https://www.google.co.in/ \""
    };
    //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.By executing the program, you will get the PDF document.

Click here to explore the rich set of Syncfusion Essential PDF features.

 

Take a moment to peruse the documentation for Converting HTML to PDF, where you will find various options for URL to PDF, HTML string to PDF, and Hyperlinks.

AWS Lambda: HtmlToPDFAWSLambdaBlink.zip

Console sample: ClientHtmlToPDFConversion.zip

See Also:

Convert HTML to PDF in Azure Function

Convert HTML to PDF in Azure App Service

Convert HTML to PDF in Azure Function Linux

Convert HTML to PDF in Azure App Service Linux

Convert HTML to PDF in docker

 

Note:

Starting with v16.2.0.x, if you reference the Syncfusion assemblies from the trial setup or the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the 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
Please  to leave a comment
Access denied
Access denied