How to Generate PDF from HTML Using PdfSharp in C#
Introducing PdfSharp: A Lightweight .NET PDF Library
PdfSharp is an open-source PDF library for .NET, enabling developers to create, read, and modify PDF documents programmatically. It’s an excellent choice for generating PDF reports in your SaaS applications due to its simplicity and flexibility.
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:
Lightweight and easy to use.
Open-source with an MIT license.
Great for basic PDF creation and manipulation.
Limited support for advanced features like HTML to PDF conversion natively.
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.
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.
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:
Step-by-Step Guide: Converting HTML to PDF
Since PdfSharp doesn’t natively support HTML to PDF conversion, use it alongside HtmlRenderer.PdfSharp.
Installing HtmlRenderer.PdfSharp
Rendering HTML to PDF
Troubleshooting Common Issues
When working with PdfSharp and HTML rendering, you may encounter some common issues:
Styles Not Applying Correctly
Ensure all CSS styles are inline or within the <style> tags.
Some advanced CSS features may not be supported.
Images Not Displaying
Use absolute paths or embed images using Base64 encoding.
Example:
Unsupported HTML Elements
Not all HTML elements are supported by HtmlRenderer.PdfSharp.
Stick to basic HTML and CSS for maximum compatibility.
Font Rendering Issues
Embed fonts or use standard web-safe fonts.
Custom fonts may require additional configuration.
How to Use a PDF API to Automate PDF Creation at Scale
For SaaS platforms, automating PDF generation at scale might require offloading the heavy lifting to a PDF API. By integrating APIs like pdforge you can handle high-volume PDF generation, complex formatting, and post-processing, all from a single backend call.
This code sends a POST request to the pdforge API, receives the generated PDF, and saves it locally.
Conclusion
PdfSharp offers a straightforward way to generate PDFs in C# applications, especially when combined with HtmlRenderer for HTML to PDF conversion. It’s ideal for projects that require a lightweight and cost-effective solution. However, if you need advanced features or plan to generate PDFs at scale, you might consider other libraries like iTextSharp or cloud-based APIs like PdfForge.
Choose PdfSharp when:
You need a free and open-source solution.
Your PDF generation needs are straightforward.
You prefer a lightweight library.
Consider Other Libraries when:
You require advanced PDF features
Licensing restrictions are not an issue.
You need better support for complex HTML/CSS rendering.
Opt for Third-Party PDF APIs, like pdforge when:
You need to generate PDFs at scale.
You prefer offloading processing to a cloud service.
You require high-fidelity rendering of complex web pages.
Try for free
7-day free trial