How to Convert HTML to PDF using C#

In this step-by-step tutorial, we will learn how to convert HTML to PDF using C#. The HTML document may contain CSS styles (inline, embedded and external). The C# code example in this topic renders HTML to PDF with high fidelity of images, texts, fonts and layout of the original content.

Following are the simple steps to generate PDF from HTML with accurate styles.

Steps to Convert HTML to PDF using C#

  1. Install Aspose.Words for .NET using NuGet Package
  2. Add reference to Aspose.Words namespace
  3. Import the input HTML document into Aspose.Words’ DOM
  4. Save the document to PDF file format

In the last topic, we looked into How to Create Word Document using C#. But this topic describes how to build HTML to PDF converter for .NET application. You can use it in ASP.NET, .NET Core applications, and Windows Forms.

Code to Convert HTML to PDF using C#

The above code example in C# creates PDF from HTML without installing MS Office. The HTML to PDF conversion using above example mimics MS Word behavior. If you open the HTML document in MS Word and convert it to PDF, you will get the same output.

 English