Converting HTML content to a Portable Document Format (PDF) is a common requirement in many web and desktop applications. This can be achieved using various libraries and tools available for .NET. In this tutorial, we will explore some of the popular methods for converting HTML to PDF in .NET.
Introduction
When it comes to generating PDFs from HTML content, there are several factors to consider, such as layout, formatting, and compatibility with different browsers and devices. Some libraries may not perform well when dealing with complex layouts or tables, resulting in messy output. Therefore, choosing the right library is crucial for achieving high-quality PDF generation.
Method 1: Using HtmlRenderer.PdfSharp
HtmlRenderer.PdfSharp is a popular, open-source library that allows you to convert HTML content to PDF using PdfSharp. It is a fully managed C# code, easy to use, thread-safe, and free under the New BSD License.
To use HtmlRenderer.PdfSharp, follow these steps:
- Install the HtmlRenderer.PdfSharp NuGet package.
- Use the
PdfGenerator
class to generate a PDF from your HTML content.
Example:
using TheArtOfDev.HtmlRenderer.PdfSharp;
public static byte[] PdfSharpConvert(string html)
{
byte[] res = null;
using (MemoryStream ms = new MemoryStream())
{
var pdf = PdfGenerator.GeneratePdf(html, PdfSharp.PageSize.A4);
pdf.Save(ms);
res = ms.ToArray();
}
return res;
}
Method 2: Using Westwind.WebView.HtmlToPdf
Westwind.WebView.HtmlToPdf is another popular library that uses the WebView2 control to generate PDFs from HTML content. This library requires Windows OS and .NET 8.0 or later.
To use Westwind.WebView.HtmlToPdf, follow these steps:
- Install the Westwind.WebView.HtmlToPdf NuGet package.
- Create an instance of the
HtmlToPdfHost
class and call thePrintToPdfStreamAsync
method to generate a PDF from your HTML content.
Example:
using Westwind.WebView.HtmlToPdf;
[HttpGet("rawpdfex")]
public async Task<IActionResult> RawPdf()
{
var file = Path.GetFullPath("./HtmlSampleFile-SelfContained.html");
var pdf = new HtmlToPdfHost();
var pdfResult = await pdf.PrintToPdfStreamAsync(file, new WebViewPrintSettings { PageRanges = "1-10" });
if (pdfResult == null || !pdfResult.IsSuccess)
{
Response.StatusCode = 500;
return new JsonResult(new
{
isError = true,
message = pdfResult.Message
});
}
return new FileStreamResult(pdfResult.ResultStream, "application/pdf");
}
Avoiding wkhtmltopdf-based Solutions
While wkhtmltopdf is a popular tool for converting HTML to PDF, it has some limitations and drawbacks when used in .NET applications. Some of the reasons to avoid wkhtmltopdf-based solutions include:
- wkhtmltopdf is implemented in C++ and may cause issues when embedding it within C# code.
- It can be challenging to configure and manage in constrained environments like Azure or Elastic Beanstalk.
- It requires write access to the server, which can be a security concern.
- It uses temporary files to generate PDFs, which can lead to performance problems on slow disk I/O systems.
Conclusion
In conclusion, converting HTML to PDF in .NET applications can be achieved using various libraries and tools. HtmlRenderer.PdfSharp and Westwind.WebView.HtmlToPdf are two popular options that offer high-quality PDF generation with ease of use and compatibility with different browsers and devices. When choosing a library, consider factors like performance, security, and compatibility to ensure the best results for your application.