Convert HTML to PDF file in AWS Elastic Beanstalk

20 Jul 20266 minutes to read

The HTML to PDF converter is a .NET library for converting webpages, SVG, MHTML, and HTML to PDF documents using C#. Using this library, you can convert HTML to PDF documents using Blink in AWS Elastic Beanstalk.

Prerequisites

Version Compatibility

The Syncfusion.HtmlToPdfConverter.Net.Aws NuGet package uses the Blink rendering engine for HTML to PDF conversion. This library is compatible with .NET 8.0 and later versions. AWS Elastic Beanstalk uses a Linux environment, and Blink is fully supported on AWS Elastic Beanstalk.

Supported Inputs

The HTML to PDF converter supports the following input types:

  • HTML String: Direct HTML content.
  • URL: Web pages and online HTML content.
  • HTML Files: Local HTML files.
  • MHTML Files: Web archive (.mhtml/.mht) content.
  • Authenticated Web Pages: Pages that require cookies, form authentication, or HTTP authentication.
  • HTTP GET/POST Requests: HTML content accessed through GET or POST methods

Required Software

  • .NET 8 SDK or later
  • AWS Account: Active AWS account with Elastic Beanstalk access
  • AWS Toolkit: AWS Toolkit for Visual Studio extension installed

Register the license key

NOTE

Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or from the NuGet feed, you must add the “Syncfusion.Licensing” assembly reference and register a license key in your application. Please refer to this link for details on registering a Syncfusion® license key.

Include a license key in your HomeController.cs file before creating an HtmlToPdfConverter instance. Refer to the Syncfusion License documentation to learn about registering the Syncfusion license key in your application.

using Syncfusion.Licensing;

namespace BlinkHtmlConversion.Controllers
{
    public class HomeController : Controller
    {
        public HomeController()
        {
            // Register the Syncfusion license
            SyncfusionLicenseProvider.RegisterLicense("YOUR LICENSE KEY");
        }
    }
}

NOTE

Starting from version 29.2.4, it is no longer necessary to manually add the following command-line arguments when using the Blink rendering engine:

settings.CommandLineArguments.Add("--no-sandbox");
settings.CommandLineArguments.Add("--disable-setuid-sandbox");

These arguments are only required when using older versions of the library that depend on Blink in sandbox-restricted environments.

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

Step 2: In the configuration window, 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 into your AWS Elastic Beanstalk project from NuGet.org:
AWS Elastic Beanstalk Step3

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

using Syncfusion.Pdf;
using Syncfusion.HtmlConverter;

Step 5: Add a new button in the Index.cshtml file as follows:

@{
    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 of the HtmlToPdfConverter class. The HTML content will be scaled based on the ViewPortSize property of the BlinkConverterSettings class:

public IActionResult BlinkToPDF()
{
    // Initialize the HTML to PDF converter with Blink rendering engine
    HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter(HtmlRenderingEngine.Blink);
    // Create Blink converter settings
    BlinkConverterSettings settings = new BlinkConverterSettings();
    // Set Blink viewport size for rendering
    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 memory stream for output
    MemoryStream stream = new MemoryStream();
    // Save the document to memory stream
    document.Save(stream);
    // Return PDF file to browser
    return File(stream.ToArray(), System.Net.Mime.MediaTypeNames.Application.Pdf, "BlinkLinuxDockerAWSBeanstalk.pdf");
}

Publish the ASP.NET Core application to AWS Elastic Beanstalk using AWS Toolkit

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

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

Step 9: Provide a valid name for the environment and URL. Check whether the URL is available by clicking the Check availability button. If the requested link is available, 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 click Next in the Permissions window:
AWS Elastic Beanstalk Step11

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

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

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

Step 15: The webpage will now 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 obtain the following PDF document output:
HTML to PDF output

A complete working sample for converting 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.

You can also view the online sample to convert HTML to PDF documents in ASP.NET Core.