Articles in this section
Category / Section

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

2 mins read

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. Paste location for QtBinaries Windows
  5. Add an Export To PDF button in index.cshtml.pdf
  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.


Conclusion

I hope you enjoyed learning about how to convert HTML to PDF in ASP.NET Core Windows.

You can refer to our ASP.NET Core PDF feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET Core PDF example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please sign in to leave a comment
Access denied
Access denied