C# guidev22.0.0

Convert HTML to PDF in C#

With the ExpertPdf HTML to PDF Converter, a C# application turns a web page, an HTML file or an HTML string into a PDF document in a few lines. It works in ASP.NET Core, ASP.NET, Windows Forms, console and background services, on .NET Framework, .NET Core and .NET 5 to .NET 11.

1. Install the NuGet package

One package, no printer driver and no other software to install.

Terminal
dotnet add package ExpertPdf.HtmlToPdf.NetCore

2. Convert a URL to PDF

Create a PdfConverter and call GetPdfBytesFromUrl. The page is loaded with its CSS, images, fonts and JavaScript.

UrlToPdf.cs · C#
using ExpertPdf.HtmlToPdf;

var converter = new PdfConverter();
byte[] pdf = converter.GetPdfBytesFromUrl("https://www.example.com/");
File.WriteAllBytes("example.pdf", pdf);

3. Convert an HTML string

For HTML you build in code, such as an invoice or a report template, use GetPdfBytesFromHtmlString. Give a base URL when the HTML uses relative paths.

HtmlToPdf.cs · C#
using ExpertPdf.HtmlToPdf;

string html = "<h1>Invoice 1024</h1><p>Total: <b>$250.00</b></p>" +
              "<img src=\"images/logo.png\">";

var converter = new PdfConverter();
// The base URL resolves relative paths such as images/logo.png and CSS files
byte[] pdf = converter.GetPdfBytesFromHtmlString(html, "https://www.example.com/");
File.WriteAllBytes("invoice.pdf", pdf);

4. Set page size, margins and a footer

The options are properties of the converter. The main options are listed on the product page; the documentation describes them all.

Options.cs · C#
var converter = new PdfConverter();

// Page
converter.PdfDocumentOptions.PdfPageSize = PdfPageSize.A4;
converter.PdfDocumentOptions.PdfPageOrientation = PDFPageOrientation.Portrait;
converter.PdfDocumentOptions.LeftMargin = 20;
converter.PdfDocumentOptions.RightMargin = 20;
converter.PdfDocumentOptions.TopMargin = 20;
converter.PdfDocumentOptions.BottomMargin = 20;

// Browser window width (pixels) and a delay for content drawn by JavaScript
converter.PageWidth = 1024;
converter.ConversionDelay = 2;

// Footer with page numbers
converter.PdfDocumentOptions.ShowFooter = true;
converter.PdfFooterOptions.FooterText = "Quarterly report";
converter.PdfFooterOptions.ShowPageNumber = true;

byte[] pdf = converter.GetPdfBytesFromUrl("https://www.example.com/report");

5. Return the PDF from ASP.NET Core

Convert a page of your own site, or HTML you render, and send the bytes as a file. For Razor views see HTML to PDF in ASP.NET MVC.

ReportController.cs · ASP.NET Core MVC
using ExpertPdf.HtmlToPdf;
using Microsoft.AspNetCore.Mvc;

public class ReportController : Controller
{
    public IActionResult Pdf()
    {
        var converter = new PdfConverter();
        string url = $"{Request.Scheme}://{Request.Host}/report/print";
        byte[] pdf = converter.GetPdfBytesFromUrl(url);

        // With a file name the browser downloads it;
        // File(pdf, "application/pdf") shows it in the browser instead
        return File(pdf, "application/pdf", "report.pdf");
    }
}

HTML to PDF in C#: questions

Which NuGet package do I use?
For .NET Core and .NET 5 to .NET 11, ExpertPdf.HtmlToPdf.NetCore. For .NET Framework applications, ExpertPdfHtmlToPdf. Both have the same PdfConverter API.
Does the converter run JavaScript?
Yes. The page's JavaScript runs during the conversion. If content is drawn after the page loads, set ConversionDelay to the number of seconds to wait.
How do I convert an HTML file from disk?
Use GetPdfBytesFromHtmlFile with the path of the file, or SavePdfFromHtmlFileToFile to write the PDF straight to another file.
Can I try it for free?
Yes. The evaluation is fully functional and has no time limit. You can also test your HTML in the free online HTML to PDF converter.
Where do I put the license key?
Assign it to converter.LicenseKey before converting. Without a key the converter runs in evaluation mode.

Convert HTML to PDF in your C# application.

Download the free evaluation with C# samples for ASP.NET Core, ASP.NET, Razor Pages, Windows Forms and console applications.

Targets.NET Framework · .NET Core · .NET 5 to 11
Versionv22.0.0 · November 20, 2026