Creating PDFs from HTML files is easy -- Here's how to do it!

IronPdf.ChromePdfRender
       .StaticRenderHTMLFileAsPdf("hello-world.html")
       .SaveAs("hello-world.pdf");
Install with NuGet
green arrow pointer

PM >  Install-Package IronPdf

Get started with IronPDF

Start using IronPDF in your project today with a free trial.

First Step:
green arrow pointer



Convert HTML to PDF Example

Here we have an example of IronPDF rendering an 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:

:path=/static-assets/pdf/how-to/html-file-to-pdf/example.html
<!DOCTYPE html>
<html>
	<head>
		<title>Page Title</title>
	</head>

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

HTML

The HTML file rendered on the web is displayed below.

Code Example

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

// Initialize a ChromePdfRenderer instance to render HTML to PDF
var renderer = new ChromePdfRenderer
{
    // Configure rendering options for the PDF
    RenderingOptions = new ChromePdfRenderOptions
    {
        CssMediaType = PdfCssMediaType.Print, // Use the 'Print' CSS media type
        MarginBottom = 0, // Set bottom margin to 0
        MarginLeft = 0, // Set left margin to 0
        MarginRight = 0, // Set right margin to 0
        MarginTop = 0, // Set top margin to 0
        Timeout = 120000, // Specify a timeout of 120 seconds (in milliseconds)
    }
};

// Set a render delay of 50 milliseconds to ensure everything is loaded
renderer.RenderingOptions.WaitFor = new ChromeWaitFor
{
    RenderDelay = 50 // Delay in milliseconds before rendering starts
};

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

// Save the generated PDF to a file
pdf.SaveAs("output.pdf");
Imports IronPdf

Imports IronPdf.Engines.Chrome

Imports IronPdf.Rendering



' Initialize a ChromePdfRenderer instance to render HTML to PDF

Private renderer = New ChromePdfRenderer With {

	.RenderingOptions = New ChromePdfRenderOptions With {

		.CssMediaType = PdfCssMediaType.Print,

		.MarginBottom = 0,

		.MarginLeft = 0,

		.MarginRight = 0,

		.MarginTop = 0,

		.Timeout = 120000

	}

}



' Set a render delay of 50 milliseconds to ensure everything is loaded

renderer.RenderingOptions.WaitFor = New ChromeWaitFor With {.RenderDelay = 50}



' Create a PDF from an existing HTML file

Dim pdf = renderer.RenderHtmlFileAsPdf("example.html")



' Save the generated PDF to a file

pdf.SaveAs("output.pdf")
$vbLabelText   $csharpLabel

The RenderHtmlFileAsPdf method returns a PdfDocument object, which is a class used to hold PDF information.

Result

This is the PDF file that the code produced:

Default Chrome Print Options

In the case that a default Chrome Print Options is desired, access the DefaultChrome property of the ChromePdfRenderOptions class and assign it to the RenderingOptions. With this setting, the PDF output from IronPdf will be identical to the Chrome Print Preview.

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

// Create a new instance of the ChromePdfRenderer class
ChromePdfRenderer renderer = new ChromePdfRenderer();

// Configure the rendering options to use default Chrome rendering options
// Important to note: 'ChromePdfRenderOptions.ChromePdfDefault' is the correct
// property to set default rendering options according to IronPdf documentation.
renderer.RenderingOptions = ChromePdfRenderOptions.ChromePdfDefault;

// Note: The provided code is a simple setup example for creating an instance of
// ChromePdfRenderer and configuring it with default rendering options. The next steps,
// such as rendering HTML to a PDF, saving the PDF, or further customization, would
// depend on additional methods and properties provided by the IronPdf library.
// These steps are not included in the current example.
Imports IronPdf



' Create a new instance of the ChromePdfRenderer class

Private renderer As New ChromePdfRenderer()



' Configure the rendering options to use default Chrome rendering options

' Important to note: 'ChromePdfRenderOptions.ChromePdfDefault' is the correct

' property to set default rendering options according to IronPdf documentation.

renderer.RenderingOptions = ChromePdfRenderOptions.ChromePdfDefault



' Note: The provided code is a simple setup example for creating an instance of

' ChromePdfRenderer and configuring it with default rendering options. The next steps,

' such as rendering HTML to a PDF, saving the PDF, or further customization, would

' depend on additional methods and properties provided by the IronPdf library.

' These steps are not included in the current example.
$vbLabelText   $csharpLabel

Frequently Asked Questions

What is the library used for rendering HTML files into PDF documents using C#?

IronPDF is a library that allows developers to render HTML files into PDF documents using C#.

How do I render an HTML file to PDF using a library in C#?

To render an HTML file to PDF using IronPDF, create an instance of the ChromePdfRenderer class, use the RenderHtmlFileAsPdf method with the HTML file path, and save the resulting PdfDocument object to a file.

What is the ChromePdfRenderer class used for?

The ChromePdfRenderer class is part of the IronPDF library, used to render HTML files into PDFs by imitating the rendering engine of the Chrome browser.

How can I configure rendering options in a PDF rendering library?

You can configure rendering options in IronPDF by setting properties in the ChromePdfRenderOptions class, allowing you to customize the output PDF.

Can I use default Chrome print options with a PDF rendering library?

Yes, you can use default Chrome print options by setting the RenderingOptions property to DefaultChrome from the PdfRenderOptions class.

Is the PDF rendering library compatible with all HTML files?

IronPDF can render any HTML file that the machine has access to, and it is particularly effective if the HTML file renders correctly in Chrome.

How do I save a rendered PDF document using a library in C#?

Once a PDF document is rendered using IronPDF, you can save it to a file using the SaveAs method of the PdfDocument object.

Where can I download a library for rendering HTML to PDF?

You can download the IronPDF library from the NuGet package manager by searching for the IronPdf package.

Why should I use a library for converting HTML to PDF?

IronPDF provides a simple and effective way to convert HTML to PDF, leveraging the Chrome rendering engine to ensure high fidelity and accuracy in the output PDF.

Regan Pun
Software Engineer
Regan graduated from the University of Reading, with a BA in Electronic Engineering. Before joining Iron Software, his previous job roles had him laser-focused on single tasks; and what he most enjoys at Iron Software is the spectrum of work he gets to undertake, whether it’s adding value to sales, technical support, product development or marketing. He enjoys understanding the way developers are using the Iron Software library, and using that knowledge to continually improve documentation and develop the products.