Convert Word document to PDF in AWS Elastic Beanstalk

29 Nov 20247 minutes to read

Syncfusion® Essential® DocIO is a .NET Core Word library used to create, read, edit, and convert Word documents programmatically without Microsoft Word or interop dependencies. Using this library, you can convert a Word document to PDF in AWS Elastic Beanstalk.

Steps to convert word document to PDF in AWS Elastic Beanstalk

Step 1: Create a new ASP.NET Core Web application (Model-View-Controller) project.

Create ASP.NET Core Web application in Visual Studio

Step 2: Install the following Nuget packages in your application from Nuget.org.

Install Syncfusion.DocIORenderer.Net.Core NuGet Package
Install SkiaSharp.NativeAssets.Linux.NoDependencies v2.88.8 NuGet Package

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 3: Include the following namespaces in the HomeController.cs file.

using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
using Syncfusion.DocIORenderer;
using Syncfusion.Pdf;

Step 4: A default action method named Index will be present in HomeController.cs. Right click on Index method and select Go To View where you will be directed to its associated view page Index.cshtml.

Step 5: Add a new button in the Index.cshtml as shown below.

@{
    Html.BeginForm("ConvertWordtoPDF", "Home", FormMethod.Get);
    {
        <div>
            <input type="submit" value="Convert WordtoPDF" style="width:150px;height:27px" />
        </div>
    }
    Html.EndForm();
}

Step 6: Include the below code snippet in the Homecontroller.cs file to convert a Word document to Pdf and download it.

public IActionResult ConvertWordtoPDF()
{
    try
    {
        using (FileStream fileStreamPath = new FileStream(Path.GetFullPath("wwwroot/Data/Input.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
        {
            //Loads the template document.
            using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Docx))
            {
                //Hooks the font substitution event.
                document.FontSettings.SubstituteFont += FontSettings_SubstituteFont;
                using (DocIORenderer render = new DocIORenderer())
                {
                    // Converts Word document into PDF document. 
                    using (PdfDocument pdf = render.ConvertToPDF(document))
                    {
                        MemoryStream memoryStream = new MemoryStream();
                        //Saves the PDF file.
                        pdf.Save(memoryStream);
                        //Unhooks the font substitution event after converting to PDF.
                        document.FontSettings.SubstituteFont -= FontSettings_SubstituteFont;
                        memoryStream.Position = 0;
                        //Download PDF document in the browser
                        return File(memoryStream, "application/pdf", "Sample.pdf");
                    }                               
                }                           
            }                      
        }                 
    }
    catch (Exception ex)
    {
        ViewBag.Message = ex.ToString();
    }
    return View("Index");
}

private static void FontSettings_SubstituteFont(object sender, SubstituteFontEventArgs args)
{
    if (args.OrignalFontName == "Calibri" && args.FontStyle == FontStyle.Regular)
    {
        args.AlternateFontStream = new FileStream(Path.GetFullPath("wwwroot/Fonts/calibri.ttf"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
    }
}

Steps to publish as AWS Elastic Beanstalk

Step 1: Right-click the project and select Publish to AWS Elastic Beanstalk (Legacy) option.
Right-click the project and select the Publish option

Step 2: Select the Deployment Target as Create a new application environment and click Next button.
Deployment Target in AWS Ealastic Beanstalk

Step 3: Choose the Environment Name in the dropdown list and the URL will be automatically assign and check the URL is available, if available click next otherwise change the URL.
Application Environment in AWS Elastic Beanstalk

Step 4: Select the instance type in t3a.micro from the dropdown list and click next.
Application Environment in AWS Elastic Beanstalk

Step 5: Click the Next button to proceed further.
Application Environment in AWS Elastic Beanstalk

Step 6: Click the Next button.
Application Options in AWS Elastic Beanstalk

Step 7: Click the Deploy button to deploy the sample on AWS Elastic Beanstalk.
Deploy the sample in AWS Elastic Beanstalk

Step 8: After changing the status from Updating to Environment is healthy, click the URL.
Status check in AWS Elastic Beanstalk

Step 9: After opening the provided URL, click Convert WordtoPDF button to download the PDF document.
Click button to Convert Word to PDF

You can download a complete working sample from GitHub.

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

Word to PDF in AWS Elastic Beanstalk

Click here to explore the rich set of Syncfusion® Word library (DocIO) features.

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