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 that the machine has access to.

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronPDF with NuGet

    PM > Install-Package IronPdf

  2. Copy the code

    IronPdf.ChromePdfRender
           .StaticRenderHTMLFileAsPdf("hello-world.html")
           .SaveAs("hello-world.pdf");
  3. Deploy to test on your live environment

    Start using IronPDF in your project today with a free trial
    arrow pointer

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;


var renderer = new ChromePdfRenderer
{
    RenderingOptions = new ChromePdfRenderOptions
    {
        CssMediaType = PdfCssMediaType.Print,
        MarginBottom = 0,
        MarginLeft = 0,
        MarginRight = 0,
        MarginTop = 0,
        Timeout = 120,
    },
};
renderer.RenderingOptions.WaitFor.RenderDelay(50);

// 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
Imports IronPdf.Engines.Chrome
Imports IronPdf.Rendering


Private renderer = New ChromePdfRenderer With {
	.RenderingOptions = New ChromePdfRenderOptions With {
		.CssMediaType = PdfCssMediaType.Print,
		.MarginBottom = 0,
		.MarginLeft = 0,
		.MarginRight = 0,
		.MarginTop = 0,
		.Timeout = 120
	}
}
renderer.RenderingOptions.WaitFor.RenderDelay(50)

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

' Export to a file or Stream
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;

ChromePdfRenderer renderer = new ChromePdfRenderer();

// Configure the rendering options to default Chrome options
renderer.RenderingOptions = ChromePdfRenderOptions.DefaultChrome;
Imports IronPdf

Private renderer As New ChromePdfRenderer()

' Configure the rendering options to default Chrome options
renderer.RenderingOptions = ChromePdfRenderOptions.DefaultChrome
$vbLabelText   $csharpLabel

Ready to see what else you can do? Check out our tutorial page here: Convert PDFs

Frequently Asked Questions

How can I convert an HTML file to a PDF in C#?

You can use the IronPDF library to convert an HTML file to PDF by creating an instance of the ChromePdfRenderer class and using the RenderHtmlFileAsPdf method with the file path to generate a PdfDocument.

What steps are involved in rendering HTML to PDF using C#?

First, download the IronPDF library from NuGet. Then instantiate the ChromePdfRenderer class, configure the RenderingOptions, pass the HTML file path to the renderer, and save the generated PDF using the SaveAs method.

How can I ensure the PDF output matches the Chrome Print Preview?

Set the RenderingOptions property to DefaultChrome from the ChromePdfRenderOptions class to ensure the PDF output matches the Chrome Print Preview.

What are the advantages of using IronPDF for HTML to PDF conversion?

IronPDF leverages the Chrome rendering engine, ensuring high fidelity and accuracy. It allows developers to test HTML content in Chrome for a pixel-perfect match in the PDF output.

Can I customize the PDF output when converting HTML files?

Yes, you can customize the PDF output by configuring the RenderingOptions in IronPDF, which provides fine-tuning capabilities for the resulting PDF.

What do I need to get started with HTML to PDF conversion in C#?

To get started, download the IronPDF library from NuGet, which provides all the necessary tools to convert HTML files to PDF documents in your C# project.

How do I save the PDF document after rendering it from an HTML file?

After rendering the PDF using IronPDF, use the SaveAs method of the PdfDocument object to save the PDF to a file.

Is it possible to render any HTML file to PDF with IronPDF?

Yes, IronPDF can render any HTML file that your machine can access, provided the HTML renders correctly in a browser like Chrome.

Why is testing HTML content in Chrome recommended before PDF conversion?

Testing HTML content in Chrome is recommended because IronPDF's rendering engine is based on Chrome, ensuring that if it renders correctly in Chrome, it will be pixel-perfect in the PDF output.

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 ...Read More