Convert HTML to PDF file in ASP.NET MVC

20 Jul 20264 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 MVC applications.

Prerequisites

Version Compatibility

The Syncfusion.HtmlToPdfConverter.AspNet.Mvc5 NuGet package uses the Blink rendering engine for HTML to PDF conversion. This library is compatible with .NET Framework 4.6.2 and later versions.

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 the Global.asax.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;

protected void Application_Start()
{
    // Register the Syncfusion license
    SyncfusionLicenseProvider.RegisterLicense("YOUR LICENSE KEY");
}

Steps to convert HTML to PDF document in ASP.NET MVC

Step 1: Create a new C# ASP.NET Web Application (.NET Framework) project targeting .NET Framework 4.6.2 or later:
Create ASP.NET MVC application

Step 2: In the project configuration window, name your project and select Create:
Configuration window1
Configuration window2

Step 3: Install the Syncfusion.HtmlToPdfConverter.AspNet.Mvc5 NuGet package into your .NET application from NuGet.org:
NuGet package installation

Step 4: 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 as shown below:

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

Step 6: Add a new action method named ExportToPDF 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 ExportToPDF()
{
    // Initialize the HTML to PDF converter
    HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
    // Create Blink converter settings
    BlinkConverterSettings blinkConverterSettings = new BlinkConverterSettings();
    // Set the Blink viewport size for rendering
    blinkConverterSettings.ViewPortSize = new Syncfusion.Drawing.Size(1280, 0);
    // Assign the Blink converter settings to the HTML converter
    htmlConverter.ConverterSettings = blinkConverterSettings;
    // Convert URL to PDF document
    PdfDocument document = htmlConverter.Convert("https://www.syncfusion.com");
    // Create memory stream for file download
    MemoryStream stream = new MemoryStream();
    // Save the document to memory stream
    document.Save(stream);
    // Close the document
    document.Close();
    // Return the PDF file to the browser
    return File(stream.ToArray(), System.Net.Mime.MediaTypeNames.Application.Pdf, "HTML-to-PDF.pdf");
}

By executing the program, you will obtain a PDF document as follows:
HTML to PDF output document

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