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.
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.
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.
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.
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.
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?
ExpertPdf.HtmlToPdf.NetCore. For .NET Framework applications, ExpertPdfHtmlToPdf. Both have the same PdfConverter API.Does the converter run JavaScript?
ConversionDelay to the number of seconds to wait.How do I convert an HTML file from disk?
GetPdfBytesFromHtmlFile with the path of the file, or SavePdfFromHtmlFileToFile to write the PDF straight to another file.Can I try it for free?
Where do I put the license key?
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.