How to Render HTML File to PDF

One of the easiest ways to use IronPDF is to tell it to render an HTML file. IronPDF can render any HTML file saved on a machine.

C# NuGet Library for PDF

Install with NuGet

Install-Package IronPdf
or
C# PDF DLL

Download DLL

Download DLL

Manually install into your project

Convert HTML to PDF Example

Here we have an example of IronPDF renderig HTML file into a PDF by using the RenderHtmlFileAsPdf() method. The parameter is a filepath to a local HTML file.

This method has the advantage of allowing the developer the opportunity to test the HTML content in a browser during development. They can, in particular, test the fidelity in rendering. We recommend Chrome, as it is the web browser on which IronPDF's rendering engine is based.

If it looks right in Chrome, then it will be pixel-perfect in IronPDF as well.

Input File

This is the example.html HTML file that the code renders:

<!DOCTYPE html>
<html>
	<head>
		<title>Page Title</title>
	</head>

	<body>
		<h1>My First Heading</h1>
		<p>My first paragraph.</p>
	</body>
</html>

HTML

Code Example

:path=/static-assets/pdf/content-code-examples/how-to/html-file-to-pdf.cs
using IronPdf;

// Instantiate Renderer
var renderer = new ChromePdfRenderer();

// Create a PDF from an existing HTML file using C#
var pdf = renderer.RenderHtmlFileAsPdf("example.html");

// Export to a file or Stream
pdf.SaveAs("output.pdf");
Imports IronPdf

' Instantiate Renderer
Private renderer = New ChromePdfRenderer()

' Create a PDF from an existing HTML file using C#
Private pdf = renderer.RenderHtmlFileAsPdf("example.html")

' Export to a file or Stream
pdf.SaveAs("output.pdf")
VB   C#

Result

This is the PDF file that the code produced: