USING IRONPDF

HTML to PDF Converter C# Open Source (.NET Libraries Comparison)

Chipego
Chipego Kalinda
April 3, 2025
Share:

Introduction

Converting HTML to PDF is a common requirement in many software applications, such as generating reports, invoices, or saving web pages as PDFs. In this article, we'll explore three popular open-source libraries for HTML to PDF conversion in C#, review their strengths and limitations, and discuss why IronPDF is a better alternative in numerous instances.

HTML to PDF converter c# open source

1. PuppeteerSharp

HTML to PDF Converter C# Open Source (.NET Libraries Comparison): Figure 1 Add from PixabayUpload

or drag and drop an image here

Add image alt text

PuppeteerSharp is a .NET wrapper for Puppeteer, a headless Chromium browser. It enables developers to convert HTML documents to PDFs by leveraging the Chromium rendering engine.

PuppeteerSharp provides precise control over the rendering process. Here's an example:

using PuppeteerSharp;
using System.Threading.Tasks;
class Program
{
    static async Task Main(string[] args)
    {
        // Download Chromium
        await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultChromiumRevision);
        // Launch Browser
        using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true }))
        {
            // Open a new page
            var page = await browser.NewPageAsync();
            // Set HTML content
            await page.SetContentAsync("<html><body><h1>Hello, PuppeteerSharp!</h1></body></html>");
            // Generate PDF
            await page.PdfAsync("output.pdf");
            Console.WriteLine("PDF Generated Successfully!");
        }
    }
}
using PuppeteerSharp;
using System.Threading.Tasks;
class Program
{
    static async Task Main(string[] args)
    {
        // Download Chromium
        await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultChromiumRevision);
        // Launch Browser
        using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true }))
        {
            // Open a new page
            var page = await browser.NewPageAsync();
            // Set HTML content
            await page.SetContentAsync("<html><body><h1>Hello, PuppeteerSharp!</h1></body></html>");
            // Generate PDF
            await page.PdfAsync("output.pdf");
            Console.WriteLine("PDF Generated Successfully!");
        }
    }
}
Imports PuppeteerSharp
Imports System.Threading.Tasks
Friend Class Program
	Shared Async Function Main(ByVal args() As String) As Task
		' Download Chromium
		Await (New BrowserFetcher()).DownloadAsync(BrowserFetcher.DefaultChromiumRevision)
		' Launch Browser
		Using browser = Await Puppeteer.LaunchAsync(New LaunchOptions With {.Headless = True})
			' Open a new page
			Dim page = Await browser.NewPageAsync()
			' Set HTML content
			Await page.SetContentAsync("<html><body><h1>Hello, PuppeteerSharp!</h1></body></html>")
			' Generate PDF
			Await page.PdfAsync("output.pdf")
			Console.WriteLine("PDF Generated Successfully!")
		End Using
	End Function
End Class
$vbLabelText   $csharpLabel

Code Explanation

  1. Download Chromium: PuppeteerSharp automatically downloads the required Chromium version to ensure compatibility.

  2. Launch Browser: Start a headless instance of Chromium using Puppeteer.LaunchAsync().

  3. Set HTML Content: Load the desired HTML into the browser page using page.SetContentAsync().

  4. Generate PDF: Use the page.PdfAsync() method to generate a PDF of the rendered content.

The result is a high-quality PDF (output.pdf) that accurately replicates the HTML structure and design.

Pros

  • High Fidelity Rendering: Supports modern web technologies, including advanced CSS and JavaScript.
  • Automation Capabilities: Besides PDFs, PuppeteerSharp can automate web browsing, testing, and data extraction.
  • Active Development: PuppeteerSharp is actively maintained and regularly updated.

Cons

  • Large File Size: Requires downloading and bundling the Chromium browser, increasing deployment size.
  • Resource Intensive: Running a browser instance can be heavy on system resources, especially for large-scale applications.
  • Limited PDF-Specific Features: PuppeteerSharp focuses on rendering rather than enhancing PDFs (e.g., adding headers or footers).

2. PdfSharp

HTML to PDF Converter C# Open Source (.NET Libraries Comparison): Figure 2 Add from PixabayUpload

or drag and drop an image here

Add image alt text

PdfSharp is a powerful open-source library for creating and manipulating PDF files in C#. While it doesn't directly support HTML rendering, it excels at providing developers with tools to generate and edit PDF documents programmatically.

Key Features of PdfSharp

  1. PDF Creation: PdfSharp allows developers to generate new PDF files from scratch by defining page sizes, adding text, shapes, images, and more.

  2. Manipulation of Existing PDFs: You can modify existing PDF documents, such as merging, splitting, or extracting content.

  3. Drawing Capabilities: PdfSharp provides robust graphics capabilities for adding custom designs to PDF files using the XGraphics class.

  4. Lightweight: It is a lightweight library, making it ideal for projects where simplicity and speed are priorities.
using PdfSharp.Pdf;
using PdfSharp.Drawing;
using HtmlAgilityPack;
class Program
{
    static void Main(string[] args)
    {
        // Example HTML content
        string htmlContent = "<html><body><h1>Hello, PdfSharp!</h1><p>This is an example of HTML to PDF.</p></body></html>";
        // Parse HTML using HtmlAgilityPack (You need to add HtmlAgilityPack via NuGet)
        var htmlDoc = new HtmlDocument();
        htmlDoc.LoadHtml(htmlContent);
        // Create a new PDF document
        PdfDocument pdfDocument = new PdfDocument();
        pdfDocument.Info.Title = "HTML to PDF Example";
        // Add a new page to the document
        PdfPage page = pdfDocument.AddPage();
        XGraphics gfx = XGraphics.FromPdfPage(page);
        XFont titleFont = new XFont("Arial", 20, XFontStyle.Bold);
        XFont textFont = new XFont("Arial", 12, XFontStyle.Regular);
        // Draw the parsed HTML content
        int yPosition = 50; // Starting Y position
        foreach (var node in htmlDoc.DocumentNode.SelectNodes("//h1 | //p"))
        {
            if (node.Name == "h1")
            {
                gfx.DrawString(node.InnerText, titleFont, XBrushes.Black, new XRect(50, yPosition, page.Width - 100, page.Height - 100), XStringFormats.TopLeft);
                yPosition += 30; // Adjust spacing
            }
            else if (node.Name == "p")
            {
                gfx.DrawString(node.InnerText, textFont, XBrushes.Black, new XRect(50, yPosition, page.Width - 100, page.Height - 100), XStringFormats.TopLeft);
                yPosition += 20; // Adjust spacing
            }
        }
        // Save the PDF document
        string outputFilePath = "HtmlToPdf.pdf";
        pdfDocument.Save(outputFilePath);
        System.Console.WriteLine($"PDF file created: {outputFilePath}");
    }
}
using PdfSharp.Pdf;
using PdfSharp.Drawing;
using HtmlAgilityPack;
class Program
{
    static void Main(string[] args)
    {
        // Example HTML content
        string htmlContent = "<html><body><h1>Hello, PdfSharp!</h1><p>This is an example of HTML to PDF.</p></body></html>";
        // Parse HTML using HtmlAgilityPack (You need to add HtmlAgilityPack via NuGet)
        var htmlDoc = new HtmlDocument();
        htmlDoc.LoadHtml(htmlContent);
        // Create a new PDF document
        PdfDocument pdfDocument = new PdfDocument();
        pdfDocument.Info.Title = "HTML to PDF Example";
        // Add a new page to the document
        PdfPage page = pdfDocument.AddPage();
        XGraphics gfx = XGraphics.FromPdfPage(page);
        XFont titleFont = new XFont("Arial", 20, XFontStyle.Bold);
        XFont textFont = new XFont("Arial", 12, XFontStyle.Regular);
        // Draw the parsed HTML content
        int yPosition = 50; // Starting Y position
        foreach (var node in htmlDoc.DocumentNode.SelectNodes("//h1 | //p"))
        {
            if (node.Name == "h1")
            {
                gfx.DrawString(node.InnerText, titleFont, XBrushes.Black, new XRect(50, yPosition, page.Width - 100, page.Height - 100), XStringFormats.TopLeft);
                yPosition += 30; // Adjust spacing
            }
            else if (node.Name == "p")
            {
                gfx.DrawString(node.InnerText, textFont, XBrushes.Black, new XRect(50, yPosition, page.Width - 100, page.Height - 100), XStringFormats.TopLeft);
                yPosition += 20; // Adjust spacing
            }
        }
        // Save the PDF document
        string outputFilePath = "HtmlToPdf.pdf";
        pdfDocument.Save(outputFilePath);
        System.Console.WriteLine($"PDF file created: {outputFilePath}");
    }
}
Imports PdfSharp.Pdf
Imports PdfSharp.Drawing
Imports HtmlAgilityPack
Friend Class Program
	Shared Sub Main(ByVal args() As String)
		' Example HTML content
		Dim htmlContent As String = "<html><body><h1>Hello, PdfSharp!</h1><p>This is an example of HTML to PDF.</p></body></html>"
		' Parse HTML using HtmlAgilityPack (You need to add HtmlAgilityPack via NuGet)
		Dim htmlDoc = New HtmlDocument()
		htmlDoc.LoadHtml(htmlContent)
		' Create a new PDF document
		Dim pdfDocument As New PdfDocument()
		pdfDocument.Info.Title = "HTML to PDF Example"
		' Add a new page to the document
		Dim page As PdfPage = pdfDocument.AddPage()
		Dim gfx As XGraphics = XGraphics.FromPdfPage(page)
		Dim titleFont As New XFont("Arial", 20, XFontStyle.Bold)
		Dim textFont As New XFont("Arial", 12, XFontStyle.Regular)
		' Draw the parsed HTML content
		Dim yPosition As Integer = 50 ' Starting Y position
		For Each node In htmlDoc.DocumentNode.SelectNodes("//h1 | //p")
			If node.Name = "h1" Then
				gfx.DrawString(node.InnerText, titleFont, XBrushes.Black, New XRect(50, yPosition, page.Width - 100, page.Height - 100), XStringFormats.TopLeft)
				yPosition += 30 ' Adjust spacing
			ElseIf node.Name = "p" Then
				gfx.DrawString(node.InnerText, textFont, XBrushes.Black, New XRect(50, yPosition, page.Width - 100, page.Height - 100), XStringFormats.TopLeft)
				yPosition += 20 ' Adjust spacing
			End If
		Next node
		' Save the PDF document
		Dim outputFilePath As String = "HtmlToPdf.pdf"
		pdfDocument.Save(outputFilePath)
		System.Console.WriteLine($"PDF file created: {outputFilePath}")
	End Sub
End Class
$vbLabelText   $csharpLabel

Code Explanation

  1. HTML Parsing: The example uses HtmlAgilityPack (an open-source library for parsing and manipulating HTML) to extract text content from

    and

    tags.

  2. Drawing Content: PdfSharp's XGraphics class is used to render the parsed HTML content as text on a PDF page.

  3. Limitations: This approach works for simple HTML structures but won't handle complex layouts, styles, or JavaScript.

Pros and Cons of PdfSharp

Pros

  • Lightweight and Easy to Use: PdfSharp is intuitive and straightforward, making it ideal for developers starting with PDF generation.
  • Open-Source and Free: No licensing fees, and the source code is available for customization.
  • Custom Drawing: Provides excellent capabilities for creating PDFs from scratch with custom designs.

Cons

  • No HTML to PDF Conversion: PdfSharp does not natively support rendering HTML to PDF, requiring additional libraries for parsing HTML.
  • Limited Support for Modern Features: Does not provide advanced capabilities like interactive PDFs, digital signatures, or annotations.
  • Performance Constraints: May not be as optimized as professional libraries for large-scale or enterprise applications.

3. Pdfium.NET SDK

HTML to PDF Converter C# Open Source (.NET Libraries Comparison): Figure 3 Add from PixabayUpload

or drag and drop an image here

Add image alt text

Pdfium.NET is a comprehensive library based on the open-source PDFium project, designed for viewing, editing, and manipulating PDF files in .NET applications. It provides developers with powerful tools to create, edit, and extract content from PDFs, making it suitable for a wide range of use cases. It is basically free HTML to PDF converter library.

Key Features of Pdfium.NET SDK

  1. PDF Creation and Editing:

    • Generate PDFs from scratch or from scanned images.
    • Edit existing PDFs by adding text, images, or annotations.
  2. Text and Image Extraction:

    • Extract text and images from PDF file format documents for further processing.
    • Search for specific text within a PDF document.
  3. PDF Viewer Control:

    • Embed a standalone PDF viewer in WinForms or WPF applications.
    • Supports zooming, scrolling, bookmarks, and text search.
  4. Compatibility:

    • Works with .NET Framework, .NET Core, .NET Standard, and .NET 6+.
    • Compatible with Windows and macOS platforms.
  5. Advanced Features:

    • Merge and split PDF files.
    • Render PDFs as images for display or printing.
using Pdfium.Net.SDK;
using System;
class Program
{
    static void Main(string[] args)
    {
        // Initialize Pdfium.NET SDK
        PdfCommon.Initialize();
        // Create a new PDF document
        PdfDocument pdfDocument = PdfDocument.CreateNew();
        // Add a page to the document
        var page = pdfDocument.Pages.InsertPageAt(pdfDocument.Pages.Count, 595, 842); // A4 size in points (8.27 x 11.69 inches)
        // Add HTML content (example: parsed manually)
        var htmlContent = "<h1>Hello, Pdfium.NET SDK!</h1><p>This is an example of HTML to PDF.</p>";
        // Example: Rendering text manually (since Pdfium.NET doesn't render HTML directly)
        var font = PdfFont.CreateFont(pdfDocument, "Arial");
        page.AddText(72, 750, font, 20, "Hello, Pdfium.NET SDK!");
        page.AddText(72, 700, font, 14, "This is an example of HTML to PDF.");
        // Save the document
        string outputFilePath = "HtmlToPdfExample.pdf";
        pdfDocument.Save(outputFilePath, SaveFlags.Default);
        Console.WriteLine($"PDF created successfully: {outputFilePath}");
    }
}
using Pdfium.Net.SDK;
using System;
class Program
{
    static void Main(string[] args)
    {
        // Initialize Pdfium.NET SDK
        PdfCommon.Initialize();
        // Create a new PDF document
        PdfDocument pdfDocument = PdfDocument.CreateNew();
        // Add a page to the document
        var page = pdfDocument.Pages.InsertPageAt(pdfDocument.Pages.Count, 595, 842); // A4 size in points (8.27 x 11.69 inches)
        // Add HTML content (example: parsed manually)
        var htmlContent = "<h1>Hello, Pdfium.NET SDK!</h1><p>This is an example of HTML to PDF.</p>";
        // Example: Rendering text manually (since Pdfium.NET doesn't render HTML directly)
        var font = PdfFont.CreateFont(pdfDocument, "Arial");
        page.AddText(72, 750, font, 20, "Hello, Pdfium.NET SDK!");
        page.AddText(72, 700, font, 14, "This is an example of HTML to PDF.");
        // Save the document
        string outputFilePath = "HtmlToPdfExample.pdf";
        pdfDocument.Save(outputFilePath, SaveFlags.Default);
        Console.WriteLine($"PDF created successfully: {outputFilePath}");
    }
}
Imports Pdfium.Net.SDK
Imports System
Friend Class Program
	Shared Sub Main(ByVal args() As String)
		' Initialize Pdfium.NET SDK
		PdfCommon.Initialize()
		' Create a new PDF document
		Dim pdfDocument As PdfDocument = PdfDocument.CreateNew()
		' Add a page to the document
		Dim page = pdfDocument.Pages.InsertPageAt(pdfDocument.Pages.Count, 595, 842) ' A4 size in points (8.27 x 11.69 inches)
		' Add HTML content (example: parsed manually)
		Dim htmlContent = "<h1>Hello, Pdfium.NET SDK!</h1><p>This is an example of HTML to PDF.</p>"
		' Example: Rendering text manually (since Pdfium.NET doesn't render HTML directly)
		Dim font = PdfFont.CreateFont(pdfDocument, "Arial")
		page.AddText(72, 750, font, 20, "Hello, Pdfium.NET SDK!")
		page.AddText(72, 700, font, 14, "This is an example of HTML to PDF.")
		' Save the document
		Dim outputFilePath As String = "HtmlToPdfExample.pdf"
		pdfDocument.Save(outputFilePath, SaveFlags.Default)
		Console.WriteLine($"PDF created successfully: {outputFilePath}")
	End Sub
End Class
$vbLabelText   $csharpLabel

Code Explanation

  1. SDK Initialization: The PdfCommon.Initialize() method initializes Pdfium.NET functionalities.

  2. Creating a PDF: A new PDF document is created using PdfDocument.CreateNew().

  3. Adding Pages: Pages are inserted into the PDF with specified dimensions (e.g., A4 size).

  4. Rendering HTML Content: Since Pdfium.NET SDK does not natively support HTML rendering, you need to manually parse and render HTML elements as text, shapes, or images.

  5. Saving the PDF: The document is saved to a file path with the Save() method.

Pros

  • Allows full control over PDF creation and editing.
  • Flexible for drawing and adding text, images, and shapes.
  • Powerful capabilities for viewing and manipulating PDFs in desktop applications.

Cons

  • Does not directly convert HTML to PDF.
  • Parsing and rendering HTML manually can be complex and time-consuming.
  • Best suited for applications focusing on PDF editing and manipulation rather than HTML conversion.

Introducing IronPDF

HTML to PDF Converter C# Open Source (.NET Libraries Comparison): Figure 4 Add from PixabayUpload

or drag and drop an image here

Add image alt text

IronPDF is a professional-grade library designed for .NET developers to effortlessly convert HTML content into high-quality PDFs. Known for its reliability, advanced features, and ease of use, IronPDF streamlines the development process while delivering precise rendering and robust functionality. Here’s why IronPDF is a standout solution:

Key Features

  1. Direct HTML to PDF Conversion: create PDF documents directly using IronPDF with HTML content, including CSS and JavaScript, into fully formatted PDFs. With just a few lines of code, developers can generate PDFs from web pages, raw HTML strings, or local HTML files.

  2. Modern Rendering Capabilities: Supporting the latest web standards, IronPDF ensures accurate rendering of complex layouts, styles, and interactive elements to convert HTML pages to PDF.

  3. Advanced PDF Features: IronPDF offers extensive customization options, such as adding headers, footers, watermarks, annotations, and bookmarks. It also supports merging, splitting, and editing existing PDFs. Split PDF documents,

  4. Performance and Scalability: Optimized for both small-scale applications and enterprise environments, IronPDF delivers fast, reliable performance for projects of any size.

  5. Ease of Integration: Designed for .NET Framework and .NET Core, IronPDF integrates smoothly with C# applications, offering developers a straightforward setup process and comprehensive documentation.

Why Choose IronPDF?

IronPDF stands out among other solutions due to its combination of features, developer support, and performance. Unlike open-source alternatives that often require extensive configuration or external dependencies, IronPDF is a self-contained solution that simplifies development without sacrificing functionality. Whether it's for generating invoices, reports, or archiving web content, IronPDF empowers developers with the tools they need to achieve professional-grade results quickly and efficiently.

IronPDF is a practical choice for developers who value reliability, scalability, and ease of use in their HTML to PDF workflows

How to convert HTML to PDF using IronPDF

class Program
{
    static void Main()
    {
        // Specify license key
        License.LicenseKey = "Yoour Key";
        // Create a new HtmlToPdf object
        var Renderer = new ChromePdfRenderer();
        // Define the HTML string/ HTML code to be converted, can use html document
        string htmlContent = "<html><body><h1>IronPDF: Beter than Open source</h1></body></html>";
        // Convert pdf simple HTML string to a PDF document
        var document = Renderer.RenderHtmlAsPdf(htmlContent);
        // Save the PDF output document to a file
        document.SaveAs("html2Pdf.pdf"); // path to pdf file generated
    }
}
class Program
{
    static void Main()
    {
        // Specify license key
        License.LicenseKey = "Yoour Key";
        // Create a new HtmlToPdf object
        var Renderer = new ChromePdfRenderer();
        // Define the HTML string/ HTML code to be converted, can use html document
        string htmlContent = "<html><body><h1>IronPDF: Beter than Open source</h1></body></html>";
        // Convert pdf simple HTML string to a PDF document
        var document = Renderer.RenderHtmlAsPdf(htmlContent);
        // Save the PDF output document to a file
        document.SaveAs("html2Pdf.pdf"); // path to pdf file generated
    }
}
Friend Class Program
	Shared Sub Main()
		' Specify license key
		License.LicenseKey = "Yoour Key"
		' Create a new HtmlToPdf object
		Dim Renderer = New ChromePdfRenderer()
		' Define the HTML string/ HTML code to be converted, can use html document
		Dim htmlContent As String = "<html><body><h1>IronPDF: Beter than Open source</h1></body></html>"
		' Convert pdf simple HTML string to a PDF document
		Dim document = Renderer.RenderHtmlAsPdf(htmlContent)
		' Save the PDF output document to a file
		document.SaveAs("html2Pdf.pdf") ' path to pdf file generated
	End Sub
End Class
$vbLabelText   $csharpLabel

Code Snippet Explanation

1. License Key Setup

The program starts by setting the IronPDF license key, which is required to unlock the full functionality of the library.

2. Creating the Renderer

An instance of ChromePdfRenderer is initialized. This component is responsible for converting HTML content into a PDF document, acting as a bridge between the raw HTML and the final output.

3. Defining HTML Content

A string variable, htmlContent, is created to store the HTML structure that will be converted into a PDF. In this example, it contains a simple heading.

4. Converting HTML to PDF

The RenderHtmlAsPdf() method is called on the ChromePdfRenderer instance, passing the HTML string as input. This function processes the content and transforms it into a PDF document.

5. Saving the PDF

Finally, the generated PDF is saved to a file named "html2Pdf.pdf" using the SaveAs() method, storing it on the disk for future access.

Output PDF

HTML to PDF Converter C# Open Source (.NET Libraries Comparison): Figure 5 Add from PixabayUpload

or drag and drop an image here

Add image alt text

License Information (Trial Available)

IronPDF requires a valid license key for full functionality. You can obtain a trial license from the official website. Before using the IronPDF library, set the license key as follows:

IronPdf.License.LicenseKey = "your key";
IronPdf.License.LicenseKey = "your key";
IronPdf.License.LicenseKey = "your key"
$vbLabelText   $csharpLabel

This ensures that the library operates without limitations.

Conclusion

PuppeteerSharp is an excellent choice for developers who need precise rendering of HTML to PDF, especially when dealing with complex web pages. However, for applications that require advanced PDF-specific features, performance optimization, and ease of integration, professional tools like IronPDF are often the better option.

PdfSharp is a great choice for lightweight, programmatic PDF creation and manipulation, especially for projects with simple requirements. However, if your application requires converting HTML to PDF or advanced PDF features, IronPDF provides a more efficient and feature-rich solution.

While Pdfium.NET SDK is a robust tool for PDF manipulation, IronPDFprovides native support for direct HTML-to-PDF conversion, including rendering modern HTML, CSS, and JavaScript. IronPDF simplifies the workflow with built-in methods like HtmlToPdf.RenderHtmlAsPdf(), making it faster and more efficient for developers.

Whether it's for generating invoices, reports, or archiving web content, IronPDF empowers developers with the tools they need to achieve professional-grade results quickly and efficiently.

IronPDF is a practical choice for developers who value reliability, scalability, and ease of use in their HTML to PDF workflows

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.
NEXT >
How to Convert HTML to PDF C# Without Library