Test in production without watermarks.
Works wherever you need it to.
Get 30 days of fully functional product.
Have it up and running in minutes.
Full access to our support engineering team during your product trial
In this comprehensive tutorial, we delve into converting HTML to PDF using C# in .NET 8 with the IronPDF Library. We begin by installing the necessary packages like IronPDF and Handlebars.Net from the NuGet package manager. The tutorial guides you through setting up imports and license keys to unlock premium features.
The first task involves converting basic HTML strings into high-quality PDFs using the Chrome PDF renderer. You can render HTML pages with images, convert live websites to PDF, and set rendering options to include JavaScript and media type settings for pixel-perfect results. The tutorial further explains how to convert locally stored HTML files into professional invoices by adding margins, dynamic headers, and footers with page numbers and dates.
Customization options allow you to create HTML-based footers with custom styles and text alignment. PDF generation in landscape mode on A4 paper is also covered. The integration with Handlebars.Net for dynamic templating is highlighted, enabling the insertion of data into HTML templates before converting them to PDFs.
Additionally, the tutorial showcases merging different PDFs, like combining a cover page with content from a URL into a single document. Running the application demonstrates the efficiency of HTML to PDF conversion with IronPDF, resulting in pixel-perfect documents.
The video encourages viewers to download a trial version of IronPDF to explore its capabilities firsthand. It's a valuable resource for anyone looking to enhance their PDF generation projects in .NET 8.
// Ensure you have installed IronPDF and handlebars.net from the NuGet package manager
using IronPdf;
using HandlebarsDotNet;
using System;
namespace HtmlToPdfExample
{
class Program
{
static void Main(string[] args)
{
// A basic example of converting a simple HTML string to a PDF
var htmlToPdf = new HtmlToPdf();
// Define the HTML string to convert
string htmlString = "<html><body><h1>Hello World</h1></body></html>";
// Convert the HTML string to a PDF document
PdfDocument pdf = htmlToPdf.RenderHtmlAsPdf(htmlString);
// Save the PDF to a file
pdf.SaveAs("HelloWorld.pdf");
Console.WriteLine("PDF generated successfully!");
}
}
}
// Ensure you have installed IronPDF and handlebars.net from the NuGet package manager
using IronPdf;
using HandlebarsDotNet;
using System;
namespace HtmlToPdfExample
{
class Program
{
static void Main(string[] args)
{
// A basic example of converting a simple HTML string to a PDF
var htmlToPdf = new HtmlToPdf();
// Define the HTML string to convert
string htmlString = "<html><body><h1>Hello World</h1></body></html>";
// Convert the HTML string to a PDF document
PdfDocument pdf = htmlToPdf.RenderHtmlAsPdf(htmlString);
// Save the PDF to a file
pdf.SaveAs("HelloWorld.pdf");
Console.WriteLine("PDF generated successfully!");
}
}
}
' Ensure you have installed IronPDF and handlebars.net from the NuGet package manager
Imports IronPdf
Imports HandlebarsDotNet
Imports System
Namespace HtmlToPdfExample
Friend Class Program
Shared Sub Main(ByVal args() As String)
' A basic example of converting a simple HTML string to a PDF
Dim htmlToPdf As New HtmlToPdf()
' Define the HTML string to convert
Dim htmlString As String = "<html><body><h1>Hello World</h1></body></html>"
' Convert the HTML string to a PDF document
Dim pdf As PdfDocument = htmlToPdf.RenderHtmlAsPdf(htmlString)
' Save the PDF to a file
pdf.SaveAs("HelloWorld.pdf")
Console.WriteLine("PDF generated successfully!")
End Sub
End Class
End Namespace
Further Reading: HTML to PDF in C# .NET