We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to convert HTML to PDF in ASP.NET Core Windows

Platform: ASP.NET Core |
Control: PDF |
Published Date: February 23, 2018 |
Last Revised Date: April 26, 2019

Syncfusion HTML to PDF for .NET used to convert webpages, SVG, MHTML, and HTML to PDF. Using this library, you can convert HTML to PDF in ASP.NET Core Windows.

Steps to convert HTML to PDF in ASP.NET Core Windows programmatically:

  1. Create a new ASP.NET Core MVC application. Create new ASP.NET Core MVC application
  2. Install the Syncfusion.HtmlToPdfConverter.QtWebKit.Net.Core NuGet package as reference to your .NET Core application from NuGet.org Install required Nuget packages
  3. Copy the QtBinariesWindows folder from the installed HtmltoPdfConverter package and paste it into the folder, which contains the HTMLtoPDF_Windows.csproj file. QtBinariesWindows path location Paste location for QtBinaries Windows
  4. Then, set Copy to output directory to copy all the QtBinariesWindows (all files including inner folders and files) assemblies. Build Action
  5. Add an Export To PDF button in index.cshtml. Add Button in html page
  6. Include the following namespaces and code snippet for converting HTML to PDF in ASP.NET Core Windows.

C#

using Syncfusion.HtmlConverter;
using Syncfusion.Pdf;
using System.IO;
using Microsoft.AspNetCore.Hosting;

 

//To get content root path of the project
private readonly IHostingEnvironment _hostingEnvironment;
public HomeController(IHostingEnvironment hostingEnvironment)
{
   _hostingEnvironment = hostingEnvironment;
}
 
public IActionResult ExportToPDF()
{
//Initialize HTML to PDF converter 
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
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");
MemoryStream stream = new MemoryStream();
document.Save(stream);
return File(stream.ToArray(),   System.Net.Mime.MediaTypeNames.Application.Pdf, "Output.pdf");
}
  1. Build and run the application, the website will open in the browser, then, you can export HTML to PDF. Run the application, opened in website
  2. By exporting HTML to PDF, you will get the PDF document as follows. Output PDF document screenshot

A complete working sample can be downloaded from HTMLtoPDF_Windows.zip

Take a moment to peruse the documentation for Converting HTML to PDF, where you will find various options for URL to PDF, HTML string to PDF, and hyperlinks.

2X faster development

The ultimate ASP.NET Core UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon