|
| 1 | +--- |
| 2 | +title: Convert HTML to PDF in Amazon ECS with Fargate | Syncfusion |
| 3 | +description: Learn how to convert HTML to PDF in Amazon ECS with Fargate using Syncfusion .NET HTML converter library. |
| 4 | +platform: document-processing |
| 5 | +control: PDF |
| 6 | +documentation: UG |
| 7 | +--- |
| 8 | + |
| 9 | +# Convert HTML to PDF file in Amazon ECS with Fargate |
| 10 | + |
| 11 | +The Syncfusion<sup>®</sup> [HTML to PDF converter](https://www.syncfusion.com/document-sdk/net-pdf-library/html-to-pdf) 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 Amazon ECS with Fargate**. |
| 12 | + |
| 13 | +## Steps to convert HTML to PDF using Blink in Amazon ECS with Fargate |
| 14 | + |
| 15 | +Step 1: Create a new C# ASP.NET Core Web Application project. |
| 16 | + |
| 17 | + |
| 18 | +Step 2: In configuration windows, name your project and select **Next**. |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | +Step 3: Install the [Syncfusion.HtmlToPdfConverter.Net.Linux](https://www.nuget.org/packages/Syncfusion.HtmlToPdfConverter.Net.Linux) NuGet package as a reference to your AWS Elastic Beanstalk project from [NuGet.org.](https://www.nuget.org/). |
| 24 | + |
| 25 | + |
| 26 | +Step 4: Include the following commands in the Docker file to install the dependent packages in the docker container. |
| 27 | + |
| 28 | +{% highlight c# tabtitle="C#" %} |
| 29 | + |
| 30 | +RUN apt-get update && \ |
| 31 | +apt-get install -yq --no-install-recommends \ |
| 32 | +libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 \ |
| 33 | +libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 \ |
| 34 | +libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 \ |
| 35 | +libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 \ |
| 36 | +libnss3 libgbm1 |
| 37 | + |
| 38 | +{% endhighlight %} |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | +Step 5: 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. |
| 43 | + |
| 44 | +{% highlight c# tabtitle="C#" %} |
| 45 | + |
| 46 | +using Syncfusion.Pdf; |
| 47 | +using Syncfusion.HtmlConverter; |
| 48 | +using System.IO; |
| 49 | + |
| 50 | +{% endhighlight %} |
| 51 | + |
| 52 | +Step 6: Add a new button in **index.cshtml** as follows. |
| 53 | + |
| 54 | +{% highlight c# tabtitle="C#" %} |
| 55 | + |
| 56 | +@{ |
| 57 | + Html.BeginForm("BlinkToPDF", "Home", FormMethod.Get); |
| 58 | + { |
| 59 | + <div> |
| 60 | + <input type="submit" value="HTML To PDF" style="width:150px;height:27px" /> |
| 61 | + <br /> |
| 62 | + <div class="text-danger"> |
| 63 | + @ViewBag.Message |
| 64 | + </div> |
| 65 | + </div> |
| 66 | + } |
| 67 | + Html.EndForm(); |
| 68 | +} |
| 69 | + |
| 70 | +{% endhighlight %} |
| 71 | + |
| 72 | +Step 7: 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](https://help.syncfusion.com/cr/document-processing/Syncfusion.HtmlConverter.HtmlToPdfConverter.html) class. The HTML content will be scaled based on the given [ViewPortSize](https://help.syncfusion.com/cr/document-processing/Syncfusion.HtmlConverter.BlinkConverterSettings.html#Syncfusion_HtmlConverter_BlinkConverterSettings_ViewPortSize) property of the [BlinkConverterSettings](https://help.syncfusion.com/cr/document-processing/Syncfusion.HtmlConverter.BlinkConverterSettings.html) class. |
| 73 | + |
| 74 | +{% highlight c# tabtitle="C#" %} |
| 75 | + |
| 76 | +public IActionResult BlinkToPDF() |
| 77 | +{ |
| 78 | + //Initialize HTML to PDF converter. |
| 79 | + HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter(HtmlRenderingEngine.Blink); |
| 80 | + BlinkConverterSettings settings = new BlinkConverterSettings(); |
| 81 | + //Set Blink viewport size. |
| 82 | + settings.ViewPortSize = new Syncfusion.Drawing.Size(1280, 0); |
| 83 | + //Assign Blink settings to the HTML converter. |
| 84 | + htmlConverter.ConverterSettings = settings; |
| 85 | + //Convert URL to PDF document. |
| 86 | + PdfDocument document = htmlConverter.Convert("https://www.syncfusion.com"); |
| 87 | + //Create the memory stream. |
| 88 | + MemoryStream stream = new MemoryStream(); |
| 89 | + //Save the document to the memory stream. |
| 90 | + document.Save(stream); |
| 91 | + //Close the document |
| 92 | + document.Close(true); |
| 93 | + return File(stream.ToArray(), System.Net.Mime.MediaTypeNames.Application.Pdf, "BlinkLinuxDockerAWSBeanstalk.pdf"); |
| 94 | +} |
| 95 | + |
| 96 | +{% endhighlight %} |
| 97 | + |
| 98 | +N> 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: |
| 99 | +N> ```csharp |
| 100 | +N> settings.CommandLineArguments.Add("--no-sandbox"); |
| 101 | +N> settings.CommandLineArguments.Add("--disable-setuid-sandbox"); |
| 102 | +N> ``` |
| 103 | +N> These arguments are only required when using **older versions** of the library that depend on Blink in sandbox-restricted environments. |
| 104 | + |
| 105 | +## Publish the ASP.NET Core application to Amazon ECS with Fargate using AWS Toolkit. |
| 106 | + |
| 107 | +Step 8: Click the **Publish Container to AWS (Legacy)…** option by right-clicking the project to |
| 108 | +publish the application. |
| 109 | + |
| 110 | + |
| 111 | +Step 9: Select the AWS account profile and region to use for deployment. Ensure **Service on an ECS Cluster** is selected as the Deployment Target then click the **Next** button. |
| 112 | + |
| 113 | + |
| 114 | +Step 10: Choose **Create an empty cluster** for the ECS Cluster and provide a name for the cluster and Launch Type should be set to **FARGATE** in the Launch Configuration window. |
| 115 | + |
| 116 | + |
| 117 | +Step 11: Choose **Create New** for the service and provide a name for your service in the service Configuration window. |
| 118 | + |
| 119 | + |
| 120 | +Step 12: Select the Configure Application Load Balance and Choose **Create New** in the load balancer drop-down and provide a name for your load balancer from the Application Load Balancer Configuration window. |
| 121 | + |
| 122 | + |
| 123 | +Step 13: Choose **Create New** for the task definition and enter a name for the task. Select an **Task Role** to provide AWS credentials to your application to access AWS services in the Task Definition window and Select **Publish**. |
| 124 | + |
| 125 | + |
| 126 | + |
| 127 | +Step 14: Once the deployment process completed, the toolkit will open a view onto the cluster. Click the Load Balance **URL link** to launch the application once the Load Balance status changed to Active. |
| 128 | + |
| 129 | + |
| 130 | +Now, the webpage will open in the browser. Click the button to convert the webpage to a PDF document. |
| 131 | + |
| 132 | + |
| 133 | +By executing the program, you will get a PDF document as follows. |
| 134 | + |
| 135 | + |
| 136 | +A complete working sample for converting an HTML to PDF using Blink in Amazon ECS using AWS Fargate can be downloaded from [GitHub](https://github.com/SyncfusionExamples/html-to-pdf-csharp-examples/tree/master/AWS/NetCoreAWSFargate). |
| 137 | + |
| 138 | +Click [here](https://www.syncfusion.com/document-sdk/net-pdf-library/html-to-pdf) to explore the rich set of Syncfusion Essential PDF features. |
| 139 | + |
| 140 | +An online sample link to [convert HTML to PDF document](https://ej2.syncfusion.com/aspnetcore/PDF/HtmltoPDF#/material3) in ASP.NET Core. |
0 commit comments