Convert HTML to PDF file in AWS using C#

9 Aug 202318 minutes to read

The Syncfusion HTML to PDF converter is a .NET library for converting webpages, SVG, MHTML, and HTML to PDF using C#. The result preserves all graphics, images, text, fonts, and the layout of the original HTML document or webpage. Using this library, you can convert HTML to PDF using C# with Blink rendering engine in AWS.

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

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

AWS Lambda

Steps to convert HTML to PDF in AWS Lambda

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

Step 2: In configuration window, name the project and select Create.
Convert HTMLToPDF AWS Step2

Step 3: Select Blueprint as Empty Function and click Finish.
Convert HTMLToPDF AWS Step3

Step 4: Install the Syncfusion.HtmlToPdfConverter.Net.Aws NuGet package as a reference to your AWS lambda project from NuGet.org.
NuGet package installation

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: Using the following namespaces in the Function.cs file.

  • C#
  • using Syncfusion.HtmlConverter;
    using Syncfusion.Pdf;
    using System.IO;

    Step 6: Add the following code snippet in Function.cs to convert HTML to PDF document using Convert method in HtmlToPdfConverter class.

  • C#
  • //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 PDF Document.
    document.Save(memoryStream);
    document.Close(true);
    return Convert.ToBase64String(memoryStream.ToArray());

    Step 7: Right-click the project and select Publish to AWS Lambda.
    Convert HTMLToPDF AWS Step5

    Step 8: 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.
    Convert HTMLToPDF AWS Step6

    Step 9: 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.
    Convert HTMLToPDF AWS Step7

    Step 10: After deploying the application, Sign in your AWS account and you can see the published Lambda function in AWS console.
    Convert HTMLToPDF AWS Step8

    Steps to invoke the AWS Lambda function from the console application

    Step 1: Create a new console project.
    Convert HTMLToPDF AWS Step9

    Step 2: In project configuration windows, name the project and select Create.
    Convert HTMLToPDF AWS Step10

    Step 3: Install the AWSSDK.Core, AWSSDK.Lambda and Newtonsoft.Json package as a reference to your main application from the NuGet.org.
    Convert HTMLToPDF AWS Step11

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

  • C#
  • using Amazon;
    using Amazon.Lambda;
    using Amazon.Lambda.Model;
    using Newtonsoft.Json;
    using System.IO;

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

  • C#
  • //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");

    By executing the program, you will get the PDF document as follows.
    Convert HTMLToPDF AWS Step12

    A complete working sample can be downloaded from Github.

    AWS Lambda with NET 6 container image

    Steps to convert HTML to PDF in AWS Lambda with NET 6 container image

    Step 1: Create a new AWS Lambda project with Tests as follows.
    Convert HTMLToPDF AWS Lambda Step1

    Step 2: In configuration window, name the project and select Create.
    Convert HTMLToPDF AWS Lambda Step2

    Step 3: Select Blueprint as .NET 6 (Container Image) Function and click Finish.
    Convert HTMLToPDF AWS Lambda Step3

    Step 4: Install the Syncfusion.HtmlToPdfConverter.Net.Aws and AWSSDK.Lambda NuGet package as a reference to your AWS lambda project from NuGet.org.
    NuGet package installation

    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: Using the following namespaces in the Function.cs file.

  • C#
  • using Syncfusion.HtmlConverter;
    using Syncfusion.Pdf;
    using System.IO;

    Step 6: Add the following code sample in the Function.cs to convert HTML to PDF document using Convert method in HtmlToPdfConverter class. The Blink command line arguments based on the given CommandLineArguments property of BlinkConverterSettings class.

  • C#
  • public string FunctionHandler(string input, ILambdaContext context)
    {
       //Initialize HTML to a PDF converter with the Blink rendering engine.
       HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter(HtmlRenderingEngine.Blink);
           
       BlinkConverterSettings blinkConverterSettings = new BlinkConverterSettings();
       blinkConverterSettings.BlinkPath = Path.GetFullPath("BlinkBinariesAws");
       blinkConverterSettings.CommandLineArguments.Add("--no-sandbox");
       blinkConverterSettings.CommandLineArguments.Add("--disable-setuid-sandbox");
       blinkConverterSettings.AdditionalDelay = 3000;
       htmlConverter.ConverterSettings = blinkConverterSettings;
     
       //Convert the HTML string to PDF.
       PdfDocument document = htmlConverter.Convert(input, PathToFile());        
     
       //Save the document into a stream.
       MemoryStream memoryStream = new MemoryStream();
       //Save and close the PDFDocument.
       document.Save(memoryStream);
       document.Close(true);
       string base64 = Convert.ToBase64String(memoryStream.ToArray());
       memoryStream.Close();
       memoryStream.Dispose();
     
       return base64;
    }
    
    public static string PathToFile()
    {
       string? path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
       if (string.IsNullOrEmpty(path))
       {
          path = Environment.OSVersion.Platform == PlatformID.Unix ? @"/" : @"\";
       }
       return Environment.OSVersion.Platform == PlatformID.Unix ? string.Concat(path.Substring(5), @"/") : string.Concat(path.Substring(6), @"\");
    }

    Step 7: Create a new folder as Helper and add a class file as AWSHelper.cs. Add the following namespaces and code samples in the AWSHelper class to invoke the published AWS Lambda function using the function name and access keys.

  • C#
  • Using Amazon.Lambda;
    using Amazon.Lambda.Model;
    using Newtonsoft.Json;
    
    public class AWSHelper
    {
       public static async Task<byte[]> RunLambdaFunction(string html)
       {
          try
          {
             var AwsAccessKeyId = "awsaccessKeyID";
             var AwsSecretAccessKey = "awsSecretAccessKey";
     
             AmazonLambdaClient client = new AmazonLambdaClient(AwsAccessKeyId, AwsSecretAccessKey, Amazon.RegionEndpoint.USEast1);
             InvokeRequest invoke = new InvokeRequest
             {
                FunctionName = "AWSLambdaDockerContainer",
                InvocationType = InvocationType.RequestResponse,
                Payload = Newtonsoft.Json.JsonConvert.SerializeObject(html)
             };
             //Get the InvokeResponse from the client InvokeRequest.
             InvokeResponse response = await client.InvokeAsync(invoke);
     
             //Read the response stream.
             Console.WriteLine($"Response: {response.LogResult}");
             Console.WriteLine($"Response: {response.StatusCode}");
             Console.WriteLine($"Response: {response.FunctionError}");
             var stream = new StreamReader(response.Payload);
             JsonReader reader = new JsonTextReader(stream);
             var serilizer = new JsonSerializer();
             var responseText = serilizer.Deserialize(reader);
     
             //Convert Base64String into a PDF document.
             return Convert.FromBase64String(responseText.ToString());
          }
          catch (Exception ex)
          {
              Console.WriteLine($"Exception Occured HTMLToPDFHelper: {ex}");
          }
       return Convert.FromBase64String("");
       }
    }

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

    Convert HTMLToPDF AWS Lambda Step5

    Step 9: 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.

    Convert HTMLToPDF AWS Lambda Step6

    Step 10: 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.

    Convert HTMLToPDF AWS Lambda Step7
    Convert HTMLToPDF AWS Lambda Step8

    Step 11: After deploying the application, Sign in to your AWS account, and you can see the published Lambda function in the AWS console.

    Convert HTMLToPDF AWS Lambda Step9

    Steps to invoke the AWS Lambda function from the Test application:

    Step 12: Add the following code to invoke the AWS lambda function with the HTML string from the Function Test.

  • C#
  • public class FunctionTest
    {
        [Fact]
        public void HtmlToPDFFunction()
        {
            string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
            string filePath = Environment.OSVersion.Platform == PlatformID.Unix ? string.Concat(path.Substring(5), @"/") : string.Concat(path.Substring(6), @"\");
     
            var html = File.ReadAllText($"{filePath}/HtmlSample.html");
            byte[] base64 = null;
            base64 = AWSHelper.RunLambdaFunction(html).Result;
     
            FileStream file = new FileStream($"{filePath}/file{DateTime.Now.Ticks}.pdf", FileMode.Create, FileAccess.Write);
            var ms = new MemoryStream(base64);
            ms.WriteTo(file);
            file.Close();
            ms.Close();
        }
    }

    Step 13: Right click the test application and select Run Tests.

    Convert HTMLToPDF AWS Lambda Step10

    Step 14: By executing the program, you will get the PDF document as follows.

    Convert HTMLToPDF AWS Lambda Step11

    A complete working sample can be downloaded from Github.

    AWS Elastic Beanstalk

    Steps to convert HTML to PDF using Blink in AWS Elastic Beanstalk

    Step 1: Create a new C# ASP.NET Core Web Application project.
    AWS Elastic Beanstalk Step1

    Step 2: In configuration windows, name your project and select Next.
    AWS Elastic Beanstalk Step2

    AWS Elastic Beanstalk Step2.1

    Step 3: Install the Syncfusion.HtmlToPdfConverter.Net.Aws NuGet package as a reference to your AWS Elastic Beanstalk project from NuGet.org..
    AWS Elastic Beanstalk Step3

    Step 4: A default controller named HomeController.cs gets added to create the ASP.NET Core MVC project. Include the following namespaces in that HomeController.cs file

  • C#
  • using Syncfusion.Pdf;
    using Syncfusion.HtmlConverter;
    using System.IO;

    Step 5: Add a new button in index.cshtml as follows.

  • C#
  • @{
        Html.BeginForm("BlinkToPDF", "Home", FormMethod.Get);
        {
            <div>
                <input type="submit" value="HTML To PDF" style="width:150px;height:27px" />
                <br />
                <div class="text-danger">
                    @ViewBag.Message
                </div>
            </div>
        }
        Html.EndForm();
    }

    Step 6: Add a new action method named BlinkToPDF in HomeController.cs and include the following code example to convert HTML to PDF document using the Convert method in HtmlToPdfConverter class. The HTML content will be scaled based on the given ViewPortSize property of the BlinkConverterSettings class.

  • C#
  • public IActionResult BlinkToPDF()
    {
              //Initialize HTML to PDF converter.
              HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter(HtmlRenderingEngine.Blink);
              BlinkConverterSettings settings = new BlinkConverterSettings();
              //Set command line arguments to run without the sandbox.
              settings.CommandLineArguments.Add("--no-sandbox");
              settings.CommandLineArguments.Add("--disable-setuid-sandbox");
              //Set Blink viewport size.
              settings.ViewPortSize = new Syncfusion.Drawing.Size(1280, 0);
              //Assign Blink settings to the HTML converter.
              htmlConverter.ConverterSettings = settings;
              //Convert URL to PDF document.
              PdfDocument document = htmlConverter.Convert("https://www.syncfusion.com");
              //Create the memory stream.
              MemoryStream stream = new MemoryStream();
              //Save the document to the memory stream.
              document.Save(stream);
              return File(stream.ToArray(), System.Net.Mime.MediaTypeNames.Application.Pdf, "BlinkLinuxDockerAWSBeanstalk.pdf");
    }

    Step 7: Click the Publish to AWS Elastic Beanstalk (Legacy) option by right-clicking the project to
    publish the application in the AWS Elastic Beanstalk environment.
    AWS Elastic Beanstalk Step7

    Step 8: Select the Create a new application environment and click Next from Publish to AWS Elastic Beanstalk window.
    AWS Elastic Beanstalk Step8

    Step 9: Please give any valid name to the environment and URL text box. Check whether the URL link is available while clicking the Check availability option. If the requested link is available means,
    click NEXT in the Application Environment window.
    AWS Elastic Beanstalk Step9

    Step 10: Select t3a.micro from the Instance Type text box and select Next in the AWS Options
    Window.
    AWS Elastic Beanstalk Step10

    Step 11: Select the Roles and Next option from the Permissions window.
    AWS Elastic Beanstalk Step11

    Step 12: Click Next from the Application Options window.
    AWS Elastic Beanstalk Step12

    Step 13: Click Deploy from the Review window.
    AWS Elastic Beanstalk Step13

    Step 14: Click the URL link to launch the application once the Environment is updated successfully and
    AWS Elastic Beanstalk Step14

    Environment status is healthy.
    Step 15: Now, the webpage will open in the browser. Click the button to convert the webpage to a PDF document.
    AWS Elastic Beanstalk Step15

    By executing the program, you will get a PDF document as follows.
    HTML to PDF output

    A complete working sample for converting an HTML to PDF using Linux docker in AWS Elastic Beanstalk can be downloaded from GitHub.

    Click here to explore the rich set of Syncfusion HTML to PDF converter library features.

    An online sample link to convert HTML to PDF document in ASP.NET Core.