Convert HTML to PDF file in AWS Elastic Beanstalk

14 Mar 20244 minutes to read

The Syncfusion HTML to PDF converter is a .NET library for converting webpages, SVG, MHTML, and HTML to PDF using C#. Using this library, convert HTML to PDF document 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.