Convert HTML to PDF file in ASP.NET Core

20 Jul 202611 minutes to read

The HTML to PDF converter is a .NET library used to convert HTML or web pages to PDF documents in ASP.NET Core applications.

To quickly get started with converting HTML to PDF in ASP.NET Core using the HTML-to-PDF Library, check this video:

Prerequisites

Version Compatibility

The Syncfusion.HtmlToPdfConverter.Net.Windows NuGet package uses the Blink rendering engine for HTML to PDF conversion. This library is compatible with .NET 8.0 and later versions

Required Software

  • .NET 8 SDK or later
  • Visual Studio, Visual Studio Code, or JetBrains Rider

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

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;

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

Steps to Convert HTML to PDF in ASP.NET Core

Step 1: Create a new C# ASP.NET Core Web Application project: Create ASP.NET Core Web application

Step 2: In configuration windows, name your project and select Next. Configuration window1 Configuration window2

Step 3: Install the Syncfusion.HtmlToPdfConverter.Net.Windows NuGet package as a reference to your .NET applications from NuGet.org: NuGet package installation

Step 4: A default controller named HomeController.cs is added when creating an ASP.NET Core MVC project. Include the following namespaces in the HomeController.cs file to enable HTML-to-PDF conversion functionality:

using Syncfusion.Pdf;
using Syncfusion.HtmlConverter;

Step 5: Add a new button in the Index.cshtml file to trigger the PDF conversion:

@{Html.BeginForm("ExportToPDF", "Home", FormMethod.Post);
    {
        <div>
            <input type="submit" value="Convert HTML to PDF" style="width:150px;height:27px" />
        </div>
    }
    Html.EndForm();
}

Step 6: Add a new action method named ExportToPDF in the HomeController.cs file to convert HTML to PDF using the Convert method from the HtmlToPdfConverter class. The HTML content will be scaled based on the ViewPortSize property of the BlinkConverterSettings class:

public IActionResult ExportToPDF()
{
    // Initialize HTML to PDF converter
    HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
    // Create Blink converter settings
    BlinkConverterSettings blinkConverterSettings = new BlinkConverterSettings();
    // Set Blink viewport size
    blinkConverterSettings.ViewPortSize = new Size(1280, 0);
    // Assign Blink converter settings to the HTML converter instance
    htmlConverter.ConverterSettings = blinkConverterSettings;
    // Convert the specified URL to a PDF
    PdfDocument document = htmlConverter.Convert("https://www.syncfusion.com");
    // Create a MemoryStream to hold the PDF binary data
    MemoryStream stream = new MemoryStream();
    // Save the PDF document
    document.Save(stream);
    // Close the PDF document
    document.Close();
    // Return the PDF file
    return File(stream.ToArray(), System.Net.Mime.MediaTypeNames.Application.Pdf, "HTML-to-PDF.pdf");
}

Step 7: Build the project by clicking Build > Build Solution or pressing Ctrl+Shift+B:

Step 8: Run the application by clicking the Start button (green arrow) or pressing F5:

Step 1: Open a terminal in Visual Studio Code (press Ctrl+`) and run the following command to create a new ASP.NET Core MVC project:

# Create a new ASP.NET Core MVC project
dotnet new mvc -n CreatePdfASPNETCoreAPP

Step 2: Replace CreatePdfASPNETCoreAPP with your desired project name.

Step 3: Navigate to the project directory using the following command:

# Change to the project directory
cd CreatePdfASPNETCoreAPP

Step 4: Use the following command in the terminal to add the Syncfusion.HtmlToPdfConverter.Net.Windows NuGet package to your project:

# Add Syncfusion HTML to PDF converter for Windows
dotnet add package Syncfusion.HtmlToPdfConverter.Net.Windows

Step 5: A default controller named HomeController.cs is added when creating an ASP.NET Core MVC project. Include the following namespaces in the HomeController.cs file to enable HTML-to-PDF conversion functionality:

using Syncfusion.Pdf;
using Syncfusion.HtmlConverter;

Step 6: Add a new button in the Index.cshtml file (located in the Views/Home folder) to trigger the PDF conversion:

@{Html.BeginForm("ExportToPDF", "Home", FormMethod.Post);
    {
        <div>
            <input type="submit" value="Convert HTML to PDF" style="width:150px;height:27px" />
        </div>
    }
    Html.EndForm();
}

Step 7: Add a new action method named ExportToPDF in the HomeController.cs file to convert HTML to PDF using the Convert method from the HtmlToPdfConverter class. The HTML content will be scaled based on the ViewPortSize property of the BlinkConverterSettings class:

public IActionResult ExportToPDF()
{
    // Initialize HTML to PDF converter
    HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
    // Create Blink converter settings
    BlinkConverterSettings blinkConverterSettings = new BlinkConverterSettings();
    // Set Blink viewport size
    blinkConverterSettings.ViewPortSize = new Size(1280, 0);
    // Assign Blink converter settings to the HTML converter instance
    htmlConverter.ConverterSettings = blinkConverterSettings;
    // Convert the specified URL to a PDF
    PdfDocument document = htmlConverter.Convert("https://www.syncfusion.com");
    // Create a MemoryStream to hold the PDF binary data
    MemoryStream stream = new MemoryStream();
    // Save the PDF document
    document.Save(stream);
    // Close the PDF document
    document.Close();
    // Return the PDF file
    return File(stream.ToArray(), System.Net.Mime.MediaTypeNames.Application.Pdf, "HTML-to-PDF.pdf");
}

Step 8: Build the project by running the following command in the terminal:

# Build the project and restore all dependencies
dotnet build

Step 9: Run the application by executing the following command in the terminal:

# Run the ASP.NET Core application on the local development server
dotnet run

Step 1: Open JetBrains Rider and create a new ASP.NET Core Web application project:

  • Launch JetBrains Rider
  • Click New Solution on the welcome screen

Launch JetBrains Rider

  • In the New Solution dialog, select Project Type as Web
  • Select the target framework (e.g., .NET 8.0, .NET 9.0) and template as Web App (Model-View-Controller)
  • Enter a project name and specify the location
  • Click Create

Creating a new ASP.NET Core Web application project in JetBrains Rider

Step 2: Install the NuGet package from NuGet.org:

  • Click the NuGet icon in the Rider toolbar and type Syncfusion.HtmlToPdfConverter.Net.Windows in the search bar
  • Ensure that nuget.org is selected as the package source
  • Select the latest Syncfusion.HtmlToPdfConverter.Net.Windows NuGet package from the list
  • Click the + (Add) button to add the package

Select the Syncfusion.HtmlToPdfConverter.Net.Windows package

  • Click the Install button to complete the installation

Install the package

Step 3: A default controller named HomeController.cs is added when creating an ASP.NET Core MVC project. Include the following namespaces in the HomeController.cs file to enable HTML-to-PDF conversion functionality:

using Syncfusion.Pdf;
using Syncfusion.HtmlConverter;

Step 4: Add a new button in the Index.cshtml file to trigger the PDF conversion:

@{Html.BeginForm("ExportToPDF", "Home", FormMethod.Post);
    {
        <div>
            <input type="submit" value="Convert HTML to PDF" style="width:150px;height:27px" />
        </div>
    }
    Html.EndForm();
}

Step 5: Add a new action method named ExportToPDF in the HomeController.cs file to convert HTML to PDF using the Convert method from the HtmlToPdfConverter class. The HTML content will be scaled based on the ViewPortSize property of the BlinkConverterSettings class:

public IActionResult ExportToPDF()
{
    // Initialize HTML to PDF converter
    HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
    // Create Blink converter settings
    BlinkConverterSettings blinkConverterSettings = new BlinkConverterSettings();
    // Set Blink viewport size
    blinkConverterSettings.ViewPortSize = new Size(1280, 0);
    // Assign Blink converter settings to the HTML converter instance
    htmlConverter.ConverterSettings = blinkConverterSettings;
    // Convert the specified URL to a PDF
    PdfDocument document = htmlConverter.Convert("https://www.syncfusion.com");
    // Create a MemoryStream to hold the PDF binary data
    MemoryStream stream = new MemoryStream();
    // Save the PDF document
    document.Save(stream);
    // Close the PDF document
    document.Close();
    // Return the PDF file
    return File(stream.ToArray(), System.Net.Mime.MediaTypeNames.Application.Pdf, "HTML-to-PDF.pdf");
}

Step 6: Build the project by clicking the Build button in the toolbar or pressing Ctrl+Shift+B:

Step 7: Run the application by clicking the Run button (green arrow) in the toolbar or pressing F5:

By executing the program, the application will convert the URL and generate a PDF document:
HTML to PDF output document

A complete working sample demonstrating HTML to PDF conversion in ASP.NET Core 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.