C# HTML to PDF - A Code Example

Written by drowzyozil | Published 2022/04/15
Tech Story Tags: c | .net | html-to-pdf | c-sharp | tutorial | programming | coding | hackernoon-es

TLDRIronPDF is the ideal solution for converting HTML sites in C# and.NET core projects. IronPDF allows developers to create, alter, and retrieve PDF documents from within.NET Core and framework projects. A fully working version of the Google Chromium engine is included into IronPDF DLL to render C HTML to PDF. The IronPDF can convert HTML to.pdf form the existing HTML page to. pdf pages with text HTML and HTML content disposition. Also convert windows forms and web pages. To convert html to PDF, use the ChromePdfRenderer.renderHtmlAsPdf function.via the TL;DR App

Introduction

It can be difficult to create PDF files programmatically in .NET. The PDF document file format was created with printers in mind rather than developers in mind. And there aren't many built-in libraries or functionality for PDF production in C#.
The logic to "create PDF document" or "HTML to PDF conversion" may be simple with C# and IronPDF. Much of the PDF document design and layout may be assigned to web design personnel or done using existing HTML assets.

C# PDF Library IronPDF

IronPDF is the ideal solution for converting HTML sites in .NET and .NET core projects. It not only transforms HTML but also has a lot of other useful capabilities. IronPDF allows developers to create, alter, and retrieve PDF documents from within .NET Core and framework projects. The IronPDF package allows developers to rapidly produce or alter PDFs from HTML texts.
IronPDF allows you to read data from PDF files and inject it into your .NET applications and data storage systems automatically. You may import, move, and index content from legacy PDF document storage into your document management and business process applications.

Creating a New Project in Visual Studio

Open the Visual Studio software and go to the File menu. Select "new project" and then select console application. In this article, we are going to use a console application to generate PDF documents.
Enter the project name and select the path in the appropriate text box. Then, click the create button. Select the required .NET framework, as in the screenshot below:
The Visual Studio project will now generate the structure for the selected application, and if you have selected the console, Windows, and web application, it will open the program.cs file where you can enter the code and build/run the application.
Now we can add the library and test the program.

Install the IronPDF Library

There are many ways to Integrate IronPDF in your Visual Studio project but in this article we will discuss only two of these.
1. Using the Visual Studio NuGet Package Manager
The Visual Studio software provides the NuGet Package Manager option to install the package directly to the solution. The below screenshot shows how to open the NuGet Package Manager.
It provides the search box to show the list available package libraries from the NuGet website. In the package manager, we need to search for the keyword "IronPDF", as in the below screenshot:
From the above image, we will get the list of the related packages search list. We need to select the IronPDF option and install the package our the solution.
2. Using the Visual Studio Command-Line
In Visual Studio menu, Go to Tools-> NuGet Package manager -> Package manager console
Enter the following line in the package manager console tab:
Install-Package IronPDF
Now the package will download/install to the current project and be ready to use.

Html to pdf

IronPDF provide multiple option to convert HTML to PDF using C#. below are the code examples of how it can be done.
1. Create a PDF from HTML String in .NET C#
To convert any HTML string to PDF, we can simply use the ChromePdfRenderer.RenderHtmlAsPdf function. A fully working version of the Google Chromium engine is included into IronPDF DLL to render C HTML to PDF.
using IronPDF;

IronPdf.ChromePdfRenderer Renderer = new IronPdf.ChromePdfRenderer();
                   // add a footer too
            Renderer.RenderingOptions.TextFooter.DrawDividerLine = true;
            Renderer.RenderingOptions.TextFooter.FontFamily = "Arial";
            Renderer.RenderingOptions.TextFooter.FontSize = 16;
            Renderer.RenderingOptions.TextFooter.LeftText = "{date} {time}";
            Renderer.RenderingOptions.TextFooter.RightText = "{page} of {total-pages}";
	string htmlText = "<h1> This is Sample Pdf file</h1> <p> This is the demo for Csharp Created Pdf using IronPdf </p> <p> IronPdf is a library which provides build in functions for creating, reading <br> and manuplating pdf files with just few lines of code. </p>";
            Renderer.RenderHtmlAsPdf(htmlText).SaveAs("Created PDF.pdf");
2. URL to PDF documents
IronPDF makes it simple to convert HTML to PDF documents from existing string URL. JavaScript, Images, Forms, and CSS all have a high degree of support.
By rendering PDFs from ASP.NET URLs that support query string variables, designers and developers may collaborate more easily on PDF creation.
using IronPDF;
var Renderer = new IronPdf.ChromePdfRenderer();
using var PDF =Renderer.RenderUrlAsPdf("https://en.wikipedia.org/wiki/Portable_Document_Format");
PDF.SaveAs("wikipedia.pdf");
3. Generate PDF From Existing HTML Page(s)
IronPDF can convert html to pdf form the existing HTML page to pdf pages with text HTML and HTML content disposition. Also convert windows forms and web pages.
 using IronPDF;
var Renderer = new IronPdf.ChromePdfRenderer();
 using var PDF = Renderer.RenderHTMLFileAsPdf("Assets/TestInvoice1.html");
 var OutputPath = "Invoice.pdf";
 PDF.SaveAs(OutputPath);

Conclusion

In this article we discuss how to convert html to pdf using C# library. how to convert different type of HTML template like HTML strings, HTML page to PDF page, URL to pdf documents. with the coded examples it shows how easy it is to convert html to pdf in C# using IronPDF. Iron PDF provide free html to pdf converter version for starters to enjoy this premium tool in both C# and VB.NET platforms. To convert html file to pdf in C# code IronPDF is the best tool out there with best rendering engine out there. If need more information on how HTML to PDF conversion work using IronPDF please visit this link.
You can download the software product from thisĀ link.

Published by HackerNoon on 2022/04/15