How to Render an HTML File to PDF in C#

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");
    }
}
$vbLabelText   $csharpLabel

Further Reading: How to Render HTML File to PDF

Chipego
Software Engineer
Chipego has a natural skill for listening that helps him to comprehend customer issues, and offer intelligent solutions. He joined the Iron Software team in 2023, after studying a Bachelor of Science in Information Technology. IronPDF and IronOCR are the two products Chipego has been focusing on, but his knowledge of all products is growing daily, as he finds new ways to support customers. He enjoys how collaborative life is at Iron Software, with team members from across the company bringing their varied experience to contribute to effective, innovative solutions. When Chipego is away from his desk, he can often be found enjoying a good book or playing football.
< PREVIOUS
How to Handle HTML to PDF Page Breaks Using C#
NEXT >
How to Add Headers and Footers in PDF Using IronPDF