Convert HTML to PDF file in ASP.NET Core

22 Sep 20214 minutes to read

In your ASP.NET Core application, add the following assemblies to use Essential PDF:

  • Syncfusion.Compression.Portable.dll
  • Syncfusion.Pdf.Portable.dll
  • Syncfusion.HtmlConverter.Portable.dll

For more details, refer to this Assemblies Required documentation.

Steps to convert HTML to PDF document using WebKit in ASP.NET Core

Create a new C# ASP.NET Core Web Application project.
Convert_WebKit1

Select Web Application pattern (Model-View-Controller) for the project.
Convert_WebKit2

Install the Syncfusion.HtmlToPdfConverter.QtWebKit.Net.Core NuGet package as reference to your .NET Standard applications from NuGet.org.
Convert_WebKit3

Copy the QtBinariesWindows folder from the installed HtmlToPdfConverter package and paste it into the folder which contains the HTMLtoPDF.csproj file.
WebKitCore_BinariesImage
Convert_WebKit4

Then, set Copy to output directory to copy all the QtBinariesWindows (All files including inner folders and files) assemblies.
Convert_WebKit5

A default controller with name HomeController.cs gets added on creation of ASP.NET MVC project. Include the following namespaces in that HomeController.cs file.

  • c#
  • using Syncfusion.Pdf;
    using Syncfusion.HtmlConverter;
    using System.IO;
    using Microsoft.AspNetCore.Hosting;

    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.

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

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

    Add a new action method ExportToPDF in HomeController.cs and include the below code snippet to convert HTML to PDF file and download it.

  • c#
  • //Initialize HTML to PDF converter 
    HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter(HtmlRenderingEngine.WebKit);
    
    WebKitConverterSettings settings = new WebKitConverterSettings();
    
    //Set WebKit path
    settings.WebKitPath = Path.Combine(_hostingEnvironment.ContentRootPath, "QtBinariesWindows");
    
    //Assign WebKit settings to HTML converter
    htmlConverter.ConverterSettings = settings;
    
    //Convert URL to PDF
    PdfDocument document = htmlConverter.Convert("https://www.google.com");
    
    //Saving the PDF to the MemoryStream
    MemoryStream stream = new MemoryStream();
    
    document.Save(stream);
    
    //Download the PDF document in the browser
    return File(stream.ToArray(), System.Net.Mime.MediaTypeNames.Application.Pdf, "Output.pdf");

    A complete work sample can be downloaded from HtmlToPDF.zip

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

    Create a new C# ASP.NET Core Web Application project.
    Convert_WebKit1

    Select Web Application pattern (Model-View-Controller) for the project.
    Convert_WebKit2

    Install the Syncfusion.HtmlToPdfConverter.Blink.Net.Core.Windows NuGet package as reference to your .NET Standard applications from NuGet.org.
    Convert_Blink1

    Copy the BlinkBinariesWindows folder from the installed HtmlToPdfConverter package and paste it into the folder which contains the HTMLtoPDF.csproj file.
    BlinkCore_BinariesImage
    Convert_Blink2

    Then, set Copy to output directory to copy all the BlinkBinariesWindows (All files including inner folders and files) assemblies.
    Convert_Blink3

    A default controller with name HomeController.cs gets added on creation of ASP.NET MVC project. Include the following namespaces in that HomeController.cs file.

  • c#
  • using Syncfusion.Pdf;
    using Syncfusion.HtmlConverter;
    using System.IO;
    using Microsoft.AspNetCore.Hosting;

    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.

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

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

    Add a new action method ExportToPDF in HomeController.cs and include the below code snippet to convert HTML to PDF file and download it.

  • c#
  • //Initialize HTML to PDF converter with Blink rendering engine 
    HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter(HtmlRenderingEngine.Blink);
                
    BlinkConverterSettings settings = new BlinkConverterSettings();
                
    //Set the BlinkBinaries folder path 
    settings.BlinkPath = Path.Combine(_hostingEnvironment.ContentRootPath, "BlinkBinariesWindows");
                
    //Assign Blink settings to HTML converter
    htmlConverter.ConverterSettings = settings;
    
    //Convert URL to PDF
    PdfDocument document = htmlConverter.Convert("https://www.google.com");
    
    //Saving the PDF to the MemoryStream
    MemoryStream stream = new MemoryStream();
    
    document.Save(stream);
    
    //Download the PDF document in the browser
    return File(stream.ToArray(), System.Net.Mime.MediaTypeNames.Application.Pdf, "Output.pdf");

    A complete work sample can be downloaded from HtmlToPDF.zip

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