HOWTO · Csharp
HTML to PDF in C#
Convert an HTML string to an A4 PDF in C# with HtmlRenderer.PdfSharp.
On this page
Use HtmlRenderer.PdfSharp in a C# project to render an HTML string into PDF pages, then save the document to a writable path. The package connects an HTML renderer to PdfSharp. Basic styling may render, while complex layouts and advanced CSS can differ from browser output.
Getting Started with HtmlRenderer.PdfSharp
Install HtmlRenderer.PdfSharp with Visual Studio’s NuGet Package Manager, or run dotnet add package HtmlRenderer.PdfSharp from the directory containing your project file. NuGet selects a compatible package version. If your project requires a pinned version, check that version’s target frameworks against your project before choosing it.
The package-based conversion requires the NuGet assemblies in your application project. Its API uses PdfGenerator from TheArtOfDev.HtmlRenderer.PdfSharp, PdfDocument from PdfSharp.Pdf, and PageSize.A4 from PdfSharp. Keep these package references in the project that builds the converter.
Convert an HTML String to PDF
The following standalone C# program performs the conversion with the installed package: it creates a PdfDocument, passes the HTML string to PdfGenerator.AddPdfPages with PageSize.A4, saves the document to the output path, and disposes the document.
using System;
using PdfSharp;
using PdfSharp.Pdf;
using TheArtOfDev.HtmlRenderer.PdfSharp;
class Program
{
static void Main()
{
string html = "<html><body><h1>Monthly report</h1><p>Revenue increased this month.</p></body></html>";
string outputPath = "monthly-report.pdf";
using (PdfDocument document = new PdfDocument())
{
PdfGenerator.AddPdfPages(document, html, PageSize.A4);
document.Save(outputPath);
}
Console.WriteLine("Saved " + outputPath);
}
}
Saved monthly-report.pdf
Run this program in the project where HtmlRenderer.PdfSharp is installed. In your application, use a destination the process can write to; a relative filename is resolved from the current working directory. Use an absolute path when you need a fixed destination.
Render HTML with CSS and Images
Include a <style> block and image elements in the HTML string before passing it to the generator. HtmlRenderer.PdfSharp can render basic CSS, but its output is not guaranteed to match a browser. Review documents that use advanced selectors, custom fonts, or complex positioning.
An image source must be available to the application during conversion. External image URLs need network access from that process. For local images, use a source format and path supported by the package version in your project, and make sure the process can read it. If an image is missing, check that its source is correct and accessible. The available screenshot could not be confirmed against this example, so it is omitted.
Limitations and Troubleshooting
HTML-to-PDF rendering differs from opening the same page in a browser. Keep the markup well formed, use renderer-supported styling, and inspect the generated PDF for layout changes. Complex layouts and advanced CSS may not render as expected.
If the PDF is missing, check the output path and the application’s write access to its directory. If styling is missing, reduce the HTML and add CSS back in stages. If an image is missing, confirm its URL or local source is available during conversion. These checks help distinguish a path or resource-access issue from a rendering difference.
FAQ
- What does HtmlRenderer.PdfSharp do? It renders HTML content into PDF pages using PdfSharp, allowing a C# application to save documents generated from markup.
- Can I use CSS? Basic CSS is supported. Advanced CSS and complex layouts may look different from a browser, so review the resulting PDF.
- How do images work? The image source must be available to the renderer during conversion. External URLs require network access from the application; local sources require a path the process can read.
- Can I customize the PDF? Set the page size through the PDF generation API and adjust the HTML and supported styles. Check the API for other options in your installed version.
- What should I expect from complex HTML? The renderer supports a subset of HTML and CSS. Try your actual document and simplify or adjust markup when its appearance differs from your intention.