Test in production without watermarks.
Works wherever you need it to.
Get 30 days of fully functional product.
Have it up and running in minutes.
Full access to our support engineering team during your product trial
When working with PDF documents in .NET, developers often find themselves comparing various libraries to determine which one best suits their project requirements. Three popular choices in the .NET ecosystem are PDFsharp, QuestPDF,and IronPDF. Each library caters to different use cases and offers unique strengths and weaknesses. In this article, we will provide an in-depth comparison of these libraries and how they handle basic PDF generation using this URL to help you make an informed decision.
PDFsharp is an open-source library designed to create, edit, and render PDF documents. With a focus on simplicity and core PDF functionality, PDFsharp has been a reliable tool for developers looking for straightforward PDF manipulation capabilities.
Through the use of the NuGet Package Manager, it is easy to install PDFsharp into your projects, simply run the following line into the NuGet Package Manager Console:
Install-Package PDFsharp
Install-Package PDFsharp
Now, let's take a look at how PDFsharp might handle creating a new PDF document from a given URL. As PDFsharp on its own cannot handle HTML or URL to PDF conversion, we will need to integrate a web rendering engine such as HtmlRenderer.PdfSharp to help. On top of this, we will also need to use HttpClient to retrieve the HTML content from the given URL.
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using PdfSharp.Pdf;
using TheArtOfDev.HtmlRenderer.PdfSharp;
public class Program
{
static async System.Threading.Tasks.Task Main(string[] args)
{
// Ensure proper encoding support
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
string url = "https://www.apple.com";
// Fetch HTML content from the URL
using (HttpClient client = new HttpClient())
{
string htmlContent = await client.GetStringAsync(url);
// Generate PDF from the fetched HTML content
var pdf = PdfGenerator.GeneratePdf(htmlContent, PdfSharp.PageSize.A4);
pdf.Save("output.pdf");
Console.WriteLine("PDF created successfully as 'output.pdf'.");
}
}
}
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using PdfSharp.Pdf;
using TheArtOfDev.HtmlRenderer.PdfSharp;
public class Program
{
static async System.Threading.Tasks.Task Main(string[] args)
{
// Ensure proper encoding support
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
string url = "https://www.apple.com";
// Fetch HTML content from the URL
using (HttpClient client = new HttpClient())
{
string htmlContent = await client.GetStringAsync(url);
// Generate PDF from the fetched HTML content
var pdf = PdfGenerator.GeneratePdf(htmlContent, PdfSharp.PageSize.A4);
pdf.Save("output.pdf");
Console.WriteLine("PDF created successfully as 'output.pdf'.");
}
}
}
As you can see, while PDFsharp can be used to convert HTML content and web pages to PDF files, it needs the help of additional libraries and is unable to keep any of the CSS and formatting seen on the original website. While a great free PDF developer's library, it does lack some of the more advanced features seen in paid libraries such as IronPDF.
QuestPDF is a modern, open-source library focused on generating visually appealing PDFs using a fluent API. Its innovative approach to document layout and rendering makes it a strong contender for applications requiring dynamic and complex designs.
Again, thanks to this library being available to install as a NuGet package, installing it is a straightforward process, requiring just one line to be run within the NuGet Console:
Install-Package QuestPDF
Install-Package QuestPDF
Now its time to look at how QuestPDF will handle converting our example URL to PDF format. While QuestPDF does not natively support URL-to-PDF conversion, you can use an HTTP client to fetch the HTML content and render it into a PDF using QuestPDF's fluent API.
using System;
using System.Net.Http;
using System.Threading.Tasks;
using HtmlAgilityPack;
using QuestPDF.Fluent;
using QuestPDF.Helpers;
using QuestPDF.Infrastructure;
public class Program
{
static async System.Threading.Tasks.Task Main(string[] args)
{
QuestPDF.Settings.License = LicenseType.Community;
string url = "https://www.apple.com";
// Fetch HTML content from the URL
using (HttpClient client = new HttpClient())
{
string htmlContent = await client.GetStringAsync(url);
// Parse the HTML content using HtmlAgilityPack
var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(htmlContent);
// Extract meaningful content (e.g., text inside <body>)
var bodyContent = htmlDoc.DocumentNode.SelectSingleNode("//body")?.InnerText ?? "No content found";
// Generate PDF using QuestPDF
Document.Create(container =>
{
container.Page(page =>
{
page.Size(PageSizes.A4);
page.Margin(20);
page.Content().Text(bodyContent);
});
}).GeneratePdf("output.pdf");
Console.WriteLine("PDF created successfully as 'output.pdf'.");
}
}
}
using System;
using System.Net.Http;
using System.Threading.Tasks;
using HtmlAgilityPack;
using QuestPDF.Fluent;
using QuestPDF.Helpers;
using QuestPDF.Infrastructure;
public class Program
{
static async System.Threading.Tasks.Task Main(string[] args)
{
QuestPDF.Settings.License = LicenseType.Community;
string url = "https://www.apple.com";
// Fetch HTML content from the URL
using (HttpClient client = new HttpClient())
{
string htmlContent = await client.GetStringAsync(url);
// Parse the HTML content using HtmlAgilityPack
var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(htmlContent);
// Extract meaningful content (e.g., text inside <body>)
var bodyContent = htmlDoc.DocumentNode.SelectSingleNode("//body")?.InnerText ?? "No content found";
// Generate PDF using QuestPDF
Document.Create(container =>
{
container.Page(page =>
{
page.Size(PageSizes.A4);
page.Margin(20);
page.Content().Text(bodyContent);
});
}).GeneratePdf("output.pdf");
Console.WriteLine("PDF created successfully as 'output.pdf'.");
}
}
}
Much like we saw with PDFSharp, while QuestPDF can be utilized to convert HTML content to PDF with the help of external libraries such as HtmlAgilityPack, it is unable to maintain any of the CSS styling and formatting. While QuestPDF is a great choice for anyone wanting to create PDF documents from scratch, HTML to PDF conversion is not a strong point for this library.
IronPDF is a robust PDF library designed for .NET developers who need advanced and comprehensive PDF capabilities. Its focus on HTML-to-PDF rendering, combined with additional features like advanced options for manipulating PDF documents, encryption, and PDF/A compliance, makes it a powerful choice for enterprise-grade applications.
To install IronPDF, use the NuGet Package Manager in Visual Studio:
Using the NuGet Package Manager:
Using the NuGet Package Manager Console:
Install-Package IronPdf
Install-Package IronPdf
IronPDF provides a simple and direct API for converting URLs to PDFs. Here's an example:
using IronPdf;
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderUrlAsPdf("https://www.apple.com");
pdf.SaveAs("url.pdf");
using IronPdf;
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderUrlAsPdf("https://www.apple.com");
pdf.SaveAs("url.pdf");
The above code example demonstrates how IronPDF can easily convert URL content to PDF with just a couple of lines of code. As you can see, not only has it converted the HTML, but it has also converted the CSS styling and layouts, ensuring the output PDF stays true to the original formatting. Through the use of the ChromePdfRenderer rendering engine, IronPDF produces pixel-perfect PDF documents that don't lose any of the content's original quality in the conversion process.
As seen here, IronPDF is a strong choice for HTML to PDF conversion, along with the conversion of other document types to PDF. It handles high-quality conversion jobs in a concise, easy-to-implement block of code.
PDFsharp is straightforward and easy to use, making it an excellent choice for beginners or developers with basic PDF needs. Its procedural approach allows quick setup and implementation for tasks like adding text, shapes, or images to a PDF. However, it does require an external library in order to handle HTML and URL to PDF conversion tasks.
QuestPDF, on the other hand, offers a more modern approach with its fluent API. Developers with experience in web or UI design will find it intuitive, but newcomers may face a slight learning curve as they get accustomed to the layout-centric design philosophy. But once again, it lacks the built in tools to handle HTML to PDF conversions.
IronPDF provides the easiest experience for HTML to PDF conversion and advanced PDF features. Its intuitive API reduces the time to implement and offers robust documentation and examples. IronPDF can handle the conversion of many different document types to PDF, such as the URL to PDF example we looked at in this article, all without any loss of document quality.
Performance is a critical factor, especially for applications that need to generate PDFs dynamically. PDFsharp is adequate for small and simple documents but struggles with large-scale or complex layouts.
QuestPDF excels in rendering dynamic and visually appealing documents. It is optimized for handling structured layouts with high efficiency.
IronPDF balances performance and functionality exceptionally well, especially for applications requiring HTML rendering, CSS, and JavaScript support. It is also highly capable of handling enterprise-grade PDF tasks with ease.
Choosing the right PDF library for your .NET project depends on your specific requirements. If you need a lightweight, open-source tool for basic PDF tasks, PDFsharp is a solid option. For creating dynamic, visually appealing documents, QuestPDF stands out with its modern approach.
However, if you require advanced features, seamless HTML-to-PDF conversion, and enterprise-grade capabilities, IronPDF it has to offer.
By understanding the strengths and limitations of each library, you can make an informed decision that aligns with your project's needs.