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 have access to.
How to Render HTML File to PDF
- Download HTML File to PDF C# Library
- Instantiate the ChromePdfRenderer class
- Configure the RenderingOptions to fine tune the output PDF
- Pass the HTML file path to the renderer
- Save and download the PDF
Install with NuGet
Install-Package IronPdf
Download DLL
Manually install into your project
Install with NuGet
Install-Package IronPdf
Download DLL
Manually install into your project
Start using IronPDF in your project today with a free trial.
Check out IronPDF on Nuget for quick installation and deployment. With over 8 million downloads, it's transforming PDF with C#.
Install-Package IronPdf
Consider installing the IronPDF DLL directly. Download and manually install it for your project or GAC form: IronPdf.zip
Manually install into your project
Download DLLConvert 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:
: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>
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
{
UseMarginsOnHeaderAndFooter = UseMargins.None,
CreatePdfFormsFromHtml = false,
CssMediaType = PdfCssMediaType.Print,
CustomCssUrl = null,
EnableJavaScript = false,
Javascript = null,
JavascriptMessageListener = null,
FirstPageNumber = 0,
GrayScale = false,
HtmlHeader = null,
HtmlFooter = null,
InputEncoding = null,
MarginBottom = 0,
MarginLeft = 0,
MarginRight = 0,
MarginTop = 0,
PaperOrientation = PdfPaperOrientation.Portrait,
PaperSize = PdfPaperSize.Letter,
PrintHtmlBackgrounds = false,
TextFooter = null,
TextHeader = null,
Timeout = 0,
Title = null,
ForcePaperSize = false,
ViewPortHeight = 0,
ViewPortWidth = 0,
Zoom = 0,
FitToPaperMode = FitToPaperModes.Zoom
},
LoginCredentials = null
};
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");
IRON VB CONVERTER ERROR developers@ironsoftware.com
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 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