How to Generate PDF from HTML Using PdfSharp in C# (Updated 2025)

PdfSharp: The Go-To Lightweight .NET PDF Library
PdfSharp is an open-source .NET PDF library designed for developers to create, read, and modify PDF documents programmatically with minimal overhead.

Whether you’re building a small internal tool or a robust SaaS application, PdfSharp stands out for its simplicity, flexibility, and ease of integration—especially if you need basic PDF generation from HTML content.
You can check out the full documentation here.
Comparison Between PdfSharp and Other C# PDF Libraries
When selecting a PDF library for C#, it’s important to understand the strengths and weaknesses of each option:
PdfSharp
Lightweight and easy to use.
Open-source (MIT license) with an active community.
Ideal for basic PDF creation, manipulation, and simple HTML-to-PDF use cases (when paired with HtmlRenderer).
Limited advanced features (e.g., complex HTML/CSS rendering might require other tools).
Powerful and feature-rich.
Supports advanced PDF functionalities.
Dual-licensed; free under AGPL, which may require open-sourcing your application, or paid commercial license.
Steeper learning curve due to its extensive capabilities.
Headless Chrome .NET API.
Excellent for rendering complex HTML/CSS/JS to PDF.
Larger in size due to Chromium dependency.
Ideal for generating PDFs that require full browser rendering.
Similar to PuppeteerSharp but supports multiple browsers.
Provides high-fidelity rendering of web pages to PDF.
Also larger in size with more dependencies.
Suitable for cross-browser PDF generation needs.
We also have a full comparison between C# PDF generation libraries, that you can check out here.
Setting Up PdfSharp in Your C# Project
Quick Start: Installing PdfSharp and Dependencies
Install PdfSharp via the NuGet Package Manager:
Alternatively, use the Package Manager Console:
Configuring Your .NET Environment for Seamless PDF Generation
Add the necessary namespaces to your project:
Ensure your project targets a compatible .NET framework version, such as .NET Framework 4.6.1 or later.
Below is an additional section you can include in your blog post to compare creating an invoice PDF using PdfSharp’s native methods versus using HTML-to-PDF with HtmlRenderer.PdfSharp. This allows readers to see both approaches side by side.
Creating a Basic PDF with PdfSharp
While generating PDFs from HTML templates can be very convenient, PdfSharp also offers native functionality for drawing text, shapes, and images directly onto a PDF page. Below is a simple example replicating the same invoice structure without relying on any HTML rendering libraries.
Sample Invoice Layout Using Only PdfSharp
Key Differences from HTML Rendering
• No HTML file: Instead of converting an HTML template, we directly draw text and shapes on a PDF page using XGraphics.
• Manual Layout: You have full control over the positioning (X and Y coordinates). While flexible, it also requires more effort for complex designs.
• No CSS Handling: Styles need to be manually applied by specifying fonts, sizes, and colors. There’s no automatic support for CSS.
When to Use PdfSharp Natively
• You only need simple PDFs with custom layouts.
• HTML/CSS complexity is unnecessary or overkill.
• You want fine-grained control over every text element and shape in your PDF.
For large-scale or more intricate designs, HTML templates often save time. For precise and custom drawing, PdfSharp’s native APIs give you the most direct control.
Implementing HTML to PDF Conversion with PdfSharp
Creating a Complete Invoice HTML/CSS File for Example
Here’s a more comprehensive HTML template for an invoice:
HTML Template Engines
To inject dynamic data into the HTML template, use a template engine like Scriban.
Using Scriban to Populate the Template
Install Scriban via NuGet:
Here’s how to use Scriban to fill the variables:
Using HtmlRenderer for HTML to PDF Conversion
Although PdfSharp does not natively support HTML-to-PDF conversion, it becomes straightforward when paired with HtmlRenderer.PdfSharp. This combination allows you to render HTML (including basic CSS and inline styles) into a PDF document, all within the .NET ecosystem.
Installing HtmlRenderer.PdfSharp
Rendering HTML to PDF
Scaling PDF Generation with a Third-Party API

For larger SaaS platforms requiring automated PDF generation at scale, integrating a PDF Generation API like pdforge can offload the heavy lifting. This approach is ideal for SaaS platforms with high volumes of PDF requests.
With pdforge, you can create beautiful reports with flexible layouts and complex components with an easy-to-use opinionated no-code builder. Let the AI do the heavy lifting by generating your templates, creating custom components or even filling all the variables for you.
You can handle high-volume PDF generation from a single backend call.
Here’s an example of how to generate pdf with pdforge via an API call:
You can create your account, experience our no-code builder and create your first layout template without any upfront payment clicking here.
Conclusion
PdfSharp offers a straightforward, lightweight approach to generating PDFs in C#. When combined with HtmlRenderer.PdfSharp, you can effectively handle HTML-to-PDF conversion, making it ideal for projects requiring a free, open-source solution.
If your needs involve advanced features or large-scale rendering of complex web pages, consider exploring iTextSharp, PuppeteerSharp, Playwright.
If you don't want to waste time maintaining pdfs layouts and their infrastructure or if you don't want to keep track of best practices to generate PDFs at scale, third-party PDF APIs like pdforge will save you hours of work and deliver a high quality pdf layout.
Updated for 2025 to reflect the latest iTextSharp features and best practices.

Try for free
7-day free trial