Converting HTML into PDF is a common requirement in modern .NET applications. It is one of those tasks that seems simple until you actually try to implement it. Whether it’s for generating invoices, exporting reports, or creating printable versions of entire web pages, developers often need a reliable html to pdf converter that preserves layouts, supports CSS and JavaScript, and ensures high quality pdf documents across environments.
In the .NET ecosystem, there are several approaches to PDF creation. Some libraries focus on building PDFs from scratch, while others emphasize rendering existing HTML code. This article explores IronPDF, a NET-native PDF generation library, and PuppeteerSharp, a C# port of Google’s Puppeteer project. Both can generate a document, though their workflows and features differ.
This article will explore both, compare their strengths and trade-offs, and help you decide which tool fits your scenario best.
Setting Up the Project
Visual Studio is recommended for your project setup. Start by creating a new .NET Core console application. Installing the libraries is straightforward using NuGet Package Manager.
Let’s start with installation and setup. Getting things running is always the first hurdle, and the experience here already shows the difference between the two libraries.
IronPDF Setup
IronPDF is available on NuGet and can be installed directly into your .NET project. Run the following in your package manager console:
Install-Package IronPDF
After installation, you can immediately start converting HTML strings, URLs, or local files into PDFs without external dependencies. Once installed, IronPDF works out of the box. You don’t need a separate browser engine, external service, or additional configuration. It’s just C# code in your .NET app, which makes it particularly attractive for developers who want quick integration.
Once installed, you can start manipulating PDF documents immediately. IronPDF can convert HTML form, local HTML files, or URLs into portable document format with just a few lines of code.
PuppeteerSharp Setup
PuppeteerSharp is also distributed via NuGet:
Install-Package PuppeteerSharp
However, unlike IronPDF, PuppeteerSharp requires a Chromium browser to perform rendering. The first time you run it, PuppeteerSharp can automatically download a compatible Chromium build. This means your application will carry a browser dependency, which may increase deployment size and complexity. On the flip side, you’re getting exactly the same rendering engine as Chrome, which guarantees fidelity to modern web standards.
It guarantees fidelity for PDF format and layouts, including PDF page size and JavaScript execution.
First Conversion Example
Let’s look at the simplest use case: converting a small HTML snippet into a PDF file.
Convert HTML Content to PDF - IronPDF Example
Here’s how you can achieve it with IronPDF:
using IronPdf;
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1><p>This is a PDF generated with IronPDF.</p>");
pdf.SaveAs("ironpdf_output.pdf");
This example shows IronPDF’s biggest advantage: simplicity. A single object (ChromePdfRenderer) takes care of the conversion. You pass an HTML string, and IronPDF directly produces a rendered PDF. No need to think about launching browsers, managing sessions, or fetching executables.
The result is a clean PDF file containing the HTML you provided.
Generated PDF:
HTML String to PDF - PuppeteerSharp Example
Now let’s see the same with PuppeteerSharp:
using PuppeteerSharp;
await new BrowserFetcher().DownloadAsync();
using var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true });
using var page = await browser.NewPageAsync();
await page.SetContentAsync("<h1>Hello World</h1><p>This is a PDF generated with PuppeteerSharp.</p>");
await page.PdfAsync("puppeteer_output.pdf");
Here, the flow is different. You launch a headless Chromium instance, open a new page, set the HTML content, and then instruct it to export the page as a PDF document.
The steps are more involved, but you get the power of a real browser. Every CSS rule, every bit of JavaScript, and every pixel will render just as it would in Chrome.
The generated PDF document is as:
Key Differences
These first examples already highlight the philosophical difference:
- IronPDF is designed as a .NET library for developers who want straightforward integration. You stay entirely in managed code.
- PuppeteerSharp exposes the same workflow as browser automation. It’s more verbose but provides total fidelity to modern web rendering.
This distinction impacts everything from ease of use to deployment strategy.
Feature Comparison
Here’s a broader comparison of features you may need in real-world scenarios:
Feature | IronPDF | PuppeteerSharp |
---|---|---|
HTML to PDF | ✅ | ✅ |
DOCX to PDF | ✅ | ❌ |
RTF to PDF | ✅ | ❌ |
JavaScript execution | ✅ (full) | ✅ (full, Chromium-based) |
CSS styling | ✅ | ✅ |
PDF/A & PDF/UA Compliance | ✅ | ❌ |
Headers & Footers | ✅ | ✅ |
Automatic TOC Generation | ✅ | ❌ |
LaTeX/Math Support | ✅ | ❌ |
External Browser Dependency | ❌ | ✅ (Chromium) |
✅ = supported ❌ = not supported
From this table, it’s clear that IronPDF leans toward PDF conversion and compliance features, while PuppeteerSharp emphasizes web fidelity.
Other Ways to Create PDFs
So far, we’ve only looked at converting a simple inline HTML string into a PDF. In real-world applications, you may need to create PDFs from various sources, such as a full HTML document, a local HTML file, or even a live URL. Both IronPDF and PuppeteerSharp support these approaches, though their APIs differ slightly.
Convert from HTML File - IronPDF Examples
Convert from a local HTML file:
using IronPdf;
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlFileAsPdf("invoice.html");
pdf.SaveAs("invoice.pdf");
Here, IronPDF reads the local invoice.html file, renders it as a PDF, and saves it as invoice.pdf.
Convert from a URL:
using IronPdf;
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderUrlAsPdf("https://en.wikipedia.org/wiki/PDF");
pdf.SaveAs("report.pdf");
This example fetches the live HTML page at https://en.wikipedia.org/wiki/PDF and converts it directly into a PDF.
IronPDF makes it simple to switch between HTML strings, files, or URLs without major changes to your code.
IronPDF simplifies archiving web content and handling PDF manipulation across environments.
PuppeteerSharp Examples
Convert from a local HTML file:
using var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true });
using var page = await browser.NewPageAsync();
await page.GoToAsync(@"D:\invoice.html");
await page.PdfAsync("invoice.pdf");
In this example, PuppeteerSharp opens a local file using the file:// protocol and saves the rendered output as invoice.pdf.
This allows developers to use their own templates and produce a PDF document.
Convert from a URL:
public static async Task htmllToPDF() {
using var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true });
using var page = await browser.NewPageAsync();
await page.GoToAsync("https://en.wikipedia.org/wiki/PDF");
await page.PdfAsync("report.pdf");
}
Here, PuppeteerSharp navigates to the specified URL, fully loads the page (including JavaScript execution), and then exports it as a PDF.
Comparison: Input Methods for PDF Generation
Input Type | IronPDF Support | PuppeteerSharp Support |
---|---|---|
HTML String | ✔ Built-in RenderHtmlAsPdf()
|
✘ Requires loading content manually (e.g., SetContentAsync ) |
Local HTML File | ✔ RenderHtmlFileAsPdf()
|
✔ Supported via file:// protocol |
Remote URL | ✔ RenderUrlAsPdf()
|
✔ Supported via GoToAsync(url)
|
This section gives readers a clear overview of the different ways to generate PDFs, while also showing the trade-offs between IronPDF’s simpler methods and PuppeteerSharp’s browser-driven approach.
Adding Custom Headers and Footers
Professional reports and invoices often need consistent headers and footers across pages. Both libraries support this, but in slightly different ways.
IronPDF Example
The following example demonstrates how to render multi-page HTML to PDF using IronPDF and apply headers to specific pages in various ways:
using IronPdf;
// Instantiate Chrome PDF Renderer
var renderer = new ChromePdfRenderer();
// Multi-page HTML content with page breaks
string html = @"
<p>This is 1st Page</p>
<div style='page-break-after: always;'></div>
<p>This is 2nd Page</p>
<div style='page-break-after: always;'></div>
<p>This is 3rd Page</p>
<div style='page-break-after: always;'></div>
<p>This is 4th Page</p>
<div style='page-break-after: always;'></div>
<p>This is 5th Page</p>
<div style='page-break-after: always;'></div>
<p>This is 6th Page</p>
<div style='page-break-after: always;'></div>
<p>This is 7th Page</p>";
// Render HTML as PDF
var pdfDoc = renderer.RenderHtmlAsPdf(html);
// Create a header
var header = new HtmlHeaderFooter();
header.HtmlFragment = "THIS IS HEADER {page} of {total-pages}";
// All page indexes
var allPageIndexes = Enumerable.Range(0, pdfDoc.PageCount);
// Example 1: Even pages only
var evenPageIndexes = allPageIndexes.Where(i => i % 2 == 0);
pdfDoc.AddHtmlHeaders(header, 1, evenPageIndexes);
pdfDoc.SaveAs("EvenPages.pdf");
// Example 2: Odd pages only
var oddPageIndexes = allPageIndexes.Where(i => i % 2 != 0);
pdfDoc.AddHtmlHeaders(header, 1, oddPageIndexes);
pdfDoc.SaveAs("OddPages.pdf");
// Example 3: Last page only
var lastPageIndex = new List<int> { pdfDoc.PageCount - 1 };
pdfDoc.AddHtmlHeaders(header, 1, lastPageIndex);
pdfDoc.SaveAs("LastPageOnly.pdf");
// Example 4: First page only
var firstPageIndex = new List<int> { 0 };
pdfDoc.AddHtmlHeaders(header, 1, firstPageIndex);
pdfDoc.SaveAs("FirstPageOnly.pdf");
// Example 5: Skip first page
var skipFirstPageIndexes = allPageIndexes.Skip(1);
pdfDoc.AddHtmlHeaders(header, 1, skipFirstPageIndexes);
pdfDoc.SaveAs("SkipFirstPage.pdf");
// Example 6: Skip first page, don't count it in numbering
pdfDoc.AddHtmlHeaders(header, 0, skipFirstPageIndexes);
pdfDoc.SaveAs("SkipFirstPageAndDontCountIt.pdf");
After running this code, multiple PDFs are generated with headers applied to different page ranges, showcasing IronPDF’s flexibility in customizing headers for specific pages. The output is a polished PDF where every page includes the specified header and footer.
PuppeteerSharp Example
using var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true });
using var page = await browser.NewPageAsync();
await page.SetContentAsync("<h1>Report Body</h1>");
await page.PdfAsync("puppeteer_header_footer.pdf", new PdfOptions
{
DisplayHeaderFooter = true,
HeaderTemplate = "<span style='font-size:10px;'>Company Report</span>",
FooterTemplate = "<span style='font-size:10px;'>Page <span class='pageNumber'></span> of <span class='totalPages'></span></span>",
MarginOptions = new PuppeteerSharp.Media.MarginOptions
{
Top = "60px",
Bottom = "40px"
}
});
With PuppeteerSharp, headers and footers come from Chromium’s built-in template system. You pass HTML templates with special tokens like pageNumber and totalPages.
This feels closer to working with a browser’s print-to-PDF dialog, giving you fine-grained control over margins and styling.
Advanced HTML to PDF Conversion
Let’s push things further. What happens when you’re dealing with large, dynamic, or styled documents?
IronPDF Strengths
- Converts not only HTML but also DOCX and RTF into PDFs.
- Supports standards like PDF/A (archival) and PDF/UA (accessibility).
- Offers PDF editing features: merging, splitting, adding signatures, watermarks, and annotations.
- Fast conversion for small to medium documents because there’s no external browser overhead.
PuppeteerSharp Strengths
- Executes JavaScript in full, making it perfect for SPAs (Single Page Applications), dashboards, or charts.
- Provides true pixel-perfect rendering that matches Chrome exactly.
- Handles complex CSS, responsive layouts, animations, and modern frameworks without breaking.
- Allows browser automation tasks (authentication, cookies, navigation) before exporting as PDF.
In other words:
- Choose IronPDF if you’re working with documents.
- Choose PuppeteerSharp if you’re working with web apps that need to be printed as-is.
Performance & Usability Comparison
Here’s how they stack up in terms of developer experience and performance:
Aspect | IronPDF | PuppeteerSharp |
---|---|---|
Setup | Simple NuGet install | Requires Chromium download |
Rendering Speed | Faster for small docs | Slower (browser overhead) |
JavaScript Handling | Full | Full |
Cross-Platform | ✅ | ✅ |
Deployment | Easy (no external deps) | Heavier (Chromium required) |
Learning Curve | Low | Medium (browser concepts) |
In practice:
- IronPDF tends to be faster for static or moderately styled content.
- PuppeteerSharp may take longer to start but excels in handling modern web UIs.
Licensing
Licensing is another major factor for production use.
IronPDF: Commercial license required, though a free trial is available for testing and evaluation.
PuppeteerSharp: Open-source under MIT, free to use, but you must manage Chromium updates and dependencies yourself.
Conclusion
Both IronPDF and PuppeteerSharp can convert HTML to PDF in C#, but they cater to different developer needs.
Go with IronPDF if you want an easy-to-use, .NET-native solution with rich PDF features like DOCX conversion, PDF/A compliance, and digital signatures. It’s especially suited for enterprise reporting and workflows.
Use PuppeteerSharp if you need full JavaScript rendering and pixel-perfect output identical to Chrome. It’s ideal for exporting modern web apps, dashboards, or interactive content.
In short:
- IronPDF = document-focused, enterprise-ready
- PuppeteerSharp = browser-focused, web-faithful
For developers who want to try IronPDF, a free trial is available, and licensing is flexible for production environments. PuppeteerSharp remains a strong open-source option for those comfortable managing Chromium
Top comments (0)