DEV Community

Tayyab Ali
Tayyab Ali

Posted on

Html to Pdf C# Converter (Code Example Tutorial)

A pdf file is an electronic document that can be downloaded and stored on the computer. In this process, all the textual content from the website is converted into an image which is then formatted according to printing guidelines in Adobe Acrobat Pro DC or earlier versions. HTML to PDF Conversion is a document conversion process. It can convert HTML data into a PDF file or capture the content of an HTML page as an image. The web browser renders HTML as text on display, but it doesn't know how to do anything with it. The browser needs some assistance from another software that knows how to interpret and store the data in a format that you can view later, such as a popular one like PDF.

IronPDF: C# PDF Library

IronPDF is a .NET pdf library that can create PDF files on the fly without restoring them to other 3rd party software (i.e. Adobe Acrobat). It is compatible with all types of applications and web apps, and it provides a simple API for developers, enabling them to integrate PDF functionality into their applications. It can be used for HTML to pdf converter.

IronPDF allows you to create, edit, convert and manage PDF documents. A developer can quickly create a PDF document with IronPDF by calling the set of functions for generating the content of the document, editing the text on different pages, and then converting it into a single PDF format without having to install expensive software such as Adobe Acrobat Pro or other PDF tools.

This article will see how to convert HTML string to PDF files using IronPDF.

Create C# Project

To get started with IronPDF, we have to create a C# Project. I am using Visual Studio 2019 for this tutorial. You can choose any version that suits you, but the latest version is recommended. Follow the following steps to create a C# project:

  • Open Visual Studio 2019.
  • Click on the "Create New Project" button.
  • Select "Console Application" from the templates.
  • Give the project a name and click on the "Next" button.

Select the .NET core framework according to your project requirement. The latest version of the .NET framework is recommended.

Install the IronPDF Library

Now, it's time to install the IronPDF library in our created project. We can install the IronPDF library using multiple ways. Let's get explored it one by one:

1: NuGet Package Manager Console

IronPDF is a NuGet Package. So, we can install the IronPDF library using the package manager console. Installation of IronPDF is very easy by using the console. We have to open the console, which mostly lies at the bottom of the project. After that, we will write the following command to install the IronPDF library in the console.

Install-Package IronPDF

It will start the installation. You will see the progress in the console. After installation, our project will be ready to use in the IronPDF library.

Image description

2: NuGet Package Manager

We can install the IronPDF library by using a GUI interface of NuGet Package Manager. Open the NuGet Package Manager by clicking on the following menus Tools > NuGet Package Manager > Manage NuGet Package Solution.

Image description

Go to the Browse tab and search for IronPDF in the search bar. Select the IronPDF from the search results and click on the install button. It will begin the installation of the IronPDF library.

Image description

3: Using a DLL file

If you don't want to use package manager then you can directly import the DLL file of IronPDF into your project. You can download the DLL file from this link.

Download the DLL zip file. Extract it to a specific folder.

In the Solution Explorer, right-click on References and browse for the IronPDF DLL file.

For a more detailed installation guide, visit this link.

Add the IronPDF Namespace

Now, we have to add the IronPDF namespace in our program file to use the IronPDF library. Write the following line of code on the top of every program file where you want to use the IronPDF library.

using IronPDF;

Writing Code to Convert HTML to PDF

IronPDF offers multiple ways to convert HTML to PDF conversion.

  • Converting HTML document to PDF file
  • Converting HTML web page to PDF document
  • Converting HTML strings to PDF file

Convert HTML Document to PDF file

Now it's time to write the code to convert the HTML file to pdf file format. We can create a seprate function or we can write the follwoing code to the main function.

`var Renderer = new IronPdf.ChromePdfRenderer();
using var PDF = Renderer.RenderHTMLFileAsPdf("Invoice.html");

// HTML assets such as images, CSS, and JS will be automatically loaded.

PDF.SaveAs("Invoice.pdf"); `

The following given code takes the file path of HTML files as the parameter. We use RenderHTMLFileAsPdf function to perform conversion of HTML file to PDF document. It can be an HTML template or any other HTML file which you want to convert to a PDF file. At last, we save our PDF file using the SaveAs function.


Image description

HTML Web Page to PDF document

We can convert any HTML web page which is on the internet to a PDF document programmatically in our program. We will just need the URL of the web page to start conversion. See the following code example to convert HTML URL to PDF file:

IronPdf.ChromePdfRenderer Renderer = new IronPdf.ChromePdfRenderer();
using var Pdf = Renderer.RenderUrlAsPdf("");

The given code converts the URL to a PDF document. We use the RenderURLAsPdf function and give the URL as a parameter to the function. It automatically loads every formatting and converts the output to a PDF file. After that, we save the file using the SaveAs function.


Image description

Converting HTML strings to PDF file

Third way to convert HTML to PDF is to create PDF file from scratch using HTML code. Let's see how we can do it:

var ChromePdfRenderer = new ChromePdfRenderer();
// html to turn into pdf
var html = @"<h1>Hello World!</h1><br><p>This is IronPdf.</p>";
// turn html to pdf
using var pdf = ChromePdfRenderer.RenderHtmlAsPdf(html);
// save resulting pdf into file

In the given code, we make an HTML string first and then we pass it to the RenderHtmlAsPdf function. It converts the HTML string to a PDF document. After that, we save the output file using the SaveAs function. We can add headers and footers to our PDF document by using HTML strings too. Multiple customizations are offered by IronPDF. You can get more details about HTML to pdf converter.


Image description


IronPDF is absolutely free for the developers. You can use IronPDF for development purposes with a watermark for free. But there is a price for the production level. You can avail the 30 days free trial key without any payment. After you make up your mind to purchase the IronPDF, there is an awesome offer waiting for you. Iron Software is a huge company that offers five products IronPDF, IronXL, IronOCR, IronBARCODE, and IronWebScraper. You can buy all five software for the price of just two pieces of software. It is an amazing offer. You must avail this opportunity. Get more details from this link.

You can download the software product from this link.

Discussion (0)