Test in production without watermarks.
Works wherever you need it to.
Get 30 days of fully functional product.
Have it up and running in minutes.
Full access to our support engineering team during your product trial
In this tutorial, learn how to render HTML files to PDF using the Iron PDF library in C#. Begin by installing Iron PDF via the NuGet package manager and setting up necessary imports and license keys. Create an instance of the Chrome PDF renderer class to control settings such as margins, paper size, and CSS media types. You can customize additional options, including JavaScript execution and headers or footers. To ensure dynamic content loads, add a delay with the render delay function. Use the render HTML file as PDF method to convert HTML content to a PDF document, and save the output using the save as method. Run your application to see the rendered PDF, which mirrors the original HTML content. The tutorial encourages exploring further customization options and invites users to subscribe for more tutorials and download a trial of Iron PDF.
// Ensure you have installed IronPDF via NuGet package manager in your C# project
using IronPdf; // Import IronPdf namespace at the beginning
class Program
{
static void Main(string[] args)
{
// Set your license key (if you have one)
// Licensing.RegisterLicense("***YOUR LICENSE KEY***");
// Create an instance of ChromePdfRenderer
ChromePdfRenderer renderer = new ChromePdfRenderer();
// Customize rendering options
renderer.RenderingOptions.MarginTop = 10; // Margin at the top of the page
renderer.RenderingOptions.MarginBottom = 10; // Margin at the bottom of the page
renderer.RenderingOptions.PaperSize = PdfPaperSize.A4; // Set paper size to A4
renderer.RenderingOptions.CssMediaType = PdfCssMediaType.Screen; // Use screen media type CSS
// Optionally, set additional options such as JavaScript execution and header/footer
renderer.RenderingOptions.RenderDelay = 500; // Time in milliseconds to wait for page scripts to execute
// Convert HTML to PDF
var pdf = renderer.RenderHtmlFileAsPdf("path/to/your/file.html");
// Save the PDF to the desired location
pdf.SaveAs("output.pdf");
// Notify the completion of the PDF rendering
Console.WriteLine("PDF rendering complete. Saved as output.pdf");
}
}
// Ensure you have installed IronPDF via NuGet package manager in your C# project
using IronPdf; // Import IronPdf namespace at the beginning
class Program
{
static void Main(string[] args)
{
// Set your license key (if you have one)
// Licensing.RegisterLicense("***YOUR LICENSE KEY***");
// Create an instance of ChromePdfRenderer
ChromePdfRenderer renderer = new ChromePdfRenderer();
// Customize rendering options
renderer.RenderingOptions.MarginTop = 10; // Margin at the top of the page
renderer.RenderingOptions.MarginBottom = 10; // Margin at the bottom of the page
renderer.RenderingOptions.PaperSize = PdfPaperSize.A4; // Set paper size to A4
renderer.RenderingOptions.CssMediaType = PdfCssMediaType.Screen; // Use screen media type CSS
// Optionally, set additional options such as JavaScript execution and header/footer
renderer.RenderingOptions.RenderDelay = 500; // Time in milliseconds to wait for page scripts to execute
// Convert HTML to PDF
var pdf = renderer.RenderHtmlFileAsPdf("path/to/your/file.html");
// Save the PDF to the desired location
pdf.SaveAs("output.pdf");
// Notify the completion of the PDF rendering
Console.WriteLine("PDF rendering complete. Saved as output.pdf");
}
}
Further Reading: How to Render HTML File to PDF