In previous article, we have mentioned HTML to PDF in C# MVC using Rotativa or iTextSharp and read pdf in C#, now in this article demonstrates the conversion of HTML file to PDF using IronPDF library in C#. It is designed for people having zero knowledge about C# programming. You just need to follow the steps and get the task done.
There are many occasions when you need to convert HTML to PDF. It can be a difficult task to create PDF from HTML. To overcome all the difficulties regarding PDF, IronPDF provides a smart solution to all your problems. Besides creating PDF documents, IronPDF provides much more support for PDF documents, which helps in making the task much easier than writing a whole lot of complex code and managing dependencies along with it. In the IronPDF library, HTML to PDF conversion supports:
- HTML 4 and HTML 5 rendering
- CSS 3
- JavaScript
- Image assets & SVG assets
- Icon Fonts
- Responsive layouts
- External stylesheets
- Static and multithreaded rendering
- Loading URLs
IronPDF also supports creating the pdf from a website URL of a specific HTML webpage. However, here our main concern is to convert an HTML file to PDF file.
So, in this tutorial, we will learn how to create a PDF from a given HTML file in C#.
Requirements:
- Visual Studio 2019
- IronPDF Library
- HTML File to convert
First, you need to download and install Visual Studio 2019 from the Microsoft website. If you haven’t, please download it from this link. Once downloaded and installed, open Visual Studio 2019 and create a new project. When the project is created, install the IronPDF library from NuGet Packages. Copy the given code below and your HTML file is converted to PDF. Let's have a practical demonstration to understand it clearly.
After downloading and installing Visual Studio 2019 (not covered in this article), follow the steps.
Step 1: Create a new project
Click on create a new project.
Select Console App and click next.
Name to project to HTMLFILETOPDF and click create. You can simply name anything you want.
Step 2: Install IronPDF from Manage NuGet Packages
Right-click on the project in solution explorer and click Manage NuGet Packages. If solution explorer is not found on main screen please select it from the view tab on the top.
NuGet Window will appear. Now, click on browse and search for IronPDF.
Click Install as shown below and wait for the installation to complete. It will take a few minutes and later save you a lot of time while working with it.
Once the installation is completed, the readme file is open giving information about IronPDF and code examples. Close readme.txt and NuGet to get started with code.
Step 3: Place HTML file in the directory of the project
If you have an HTML file then simply place it inside your project folder which will be in this path: C:\Users\yourusername\source\repos\HTMLFILETOPDF\HTMLFILETOPDF.
If you do not have and HTML file or don’t know how to create, don’t worry I’ll show you how to do it.
Right click the project name in solution explorer and click on “Open folder in file explorer”. Once the file explorer is open, right click again and create a new text file. Name it to index and open.
Paste the following code in index.txt file.
<html>
<head>
<title>Html to PDF</title>
</head>
<body>
<p>This is a HTML file and you are learning how to convert HTML file to PDF document using IronPDF library in C#.</p>
</body>
</html>
Now, save the file as HTML as shown in figure below.
You have successfully created an HTML file. Now we are good to go with the rest of the code.
Note: You can save the HTML file at any other location.
Step 4: Add the backend code for Creating PDF file from HTML file
Now here comes the technical part. You might be thinking about a complex code to create pdf document from an HTML file, but its totally opposite. A person with no background of programming can also understand the code. Thanks to IronPDF library. The code is very easy and simple to use.
Firstly, add the following IronPDF library to the start of the code.
using IronPdf;
Write the following code in the main function.
var Renderer = new IronPdf.HtmlToPdf();
var PDF = Renderer.RenderHTMLFileAsPdf("C:/Users/Zeeshan/source/repos/HTMLFILETOPDF/HTMLFILETOPDF/index.html");
var OutputPath ="C:/Users/Zeeshan/source/repos/HTMLFILETOPDF/HTMLFILETOPDF/index.pdf";
PDF.SaveAs(OutputPath);
Console.WriteLine("PDF created successfully");
Note: Replace the username with yours in the path, if you are saving it the location of visual studio project folder. Don’t forget to change the path in the code, if the input file is in different location. You can also change the output path.
Step 5: Run the project
After successful build and run, PDF will be created as seen in solution explorer.
Output file:
All is done and HTML file is converted to PDF file as shown in the above steps.
I hope this was easy to understand and follow. You can also visit IronPDF FAQ to create PDF in C#.
IronPDf is a lifesaver. No need to worry about the technical aspects as the library manages most of it. You just need to copy the code from IronPDF website related to your task.
With this absolute beginner's guide, you are good to go with other stuff. If you still have any confusion, please ask in the comment section below.