HTML Files to PDF
IronPDF is a powerful .NET library capable of converting HTML files into high-quality PDF files. With IronPDF, you can render HTML files to PDF in just a couple of lines, and thanks to its support for modern web standards, the resulting PDF files will come out pixel-perfect. Leveraging IronPDF's powerful HTML file to PDF feature is easy thanks to its use of the ChromePdfRenderer
class, which handles the conversion of HTML to PDF with ease.
Steps to Convert HTML Files to PDF with IronPDF
- Install the C# IronPDF library to convert HTML to PDF
using IronPdf;
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlFileAsPdf("example.html");
pdf.SaveAs("output.pdf");
This code creates a new PDF file that has been rendered from an HTML file. To do this, we must first ensure that the IronPDF library is installed and included within your project through the using IronPdf
line. Next, initialize the ChromePdfRenderer class, which provides the functionality to render HTML content as a PDF. This class ensures that the original quality of the HTML file is not lost in the conversion process.
Once the renderer is instantiated, you can convert an existing HTML file into a PDF using the RenderHtmlFileAsPdf
method. In this example, the HTML file "example.html" is passed to the method, creating a PDF object. Finally, to save the generated PDF, use the SaveAs
method, specifying the desired file name and location. This simple process allows you to easily generate PDFs from HTML files in your C# applications.
// Ensure to include the IronPdf library in your project
using IronPdf;
class Program
{
static void Main()
{
// Instantiate the ChromePdfRenderer, which will handle the HTML to PDF conversion
var renderer = new ChromePdfRenderer();
// Render the HTML file "example.html" to a PDF document
var pdf = renderer.RenderHtmlFileAsPdf("example.html");
// Save the rendered PDF to the specified location with the name "output.pdf"
pdf.SaveAs("output.pdf");
}
}
// Ensure to include the IronPdf library in your project
using IronPdf;
class Program
{
static void Main()
{
// Instantiate the ChromePdfRenderer, which will handle the HTML to PDF conversion
var renderer = new ChromePdfRenderer();
// Render the HTML file "example.html" to a PDF document
var pdf = renderer.RenderHtmlFileAsPdf("example.html");
// Save the rendered PDF to the specified location with the name "output.pdf"
pdf.SaveAs("output.pdf");
}
}
' Ensure to include the IronPdf library in your project
Imports IronPdf
Friend Class Program
Shared Sub Main()
' Instantiate the ChromePdfRenderer, which will handle the HTML to PDF conversion
Dim renderer = New ChromePdfRenderer()
' Render the HTML file "example.html" to a PDF document
Dim pdf = renderer.RenderHtmlFileAsPdf("example.html")
' Save the rendered PDF to the specified location with the name "output.pdf"
pdf.SaveAs("output.pdf")
End Sub
End Class
Click here to view the How-to Guide, including examples, sample code, and files