ASP.NET guideWeb Forms · Core
HTML to PDF in ASP.NET
Make PDF reports from the ASP.NET pages you already have: design the page with your usual controls and CSS, then export it to PDF with the ExpertPdf HTML to PDF Converter. No report designer, no printer driver.
Export an ASPX page to PDF
Render the page to HTML with Server.Execute, in the current request and with its session, then convert the HTML string.
using System.IO; using ExpertPdf.HtmlToPdf; protected void BtnPdf_Click(object sender, EventArgs e) { // Render another page of this application to HTML, in the current request var writer = new StringWriter(); Server.Execute("InvoiceView.aspx?id=1024", writer); string html = writer.ToString(); // The base URL resolves the page's relative CSS and image paths string baseUrl = Request.Url.GetLeftPart(UriPartial.Authority) + Request.ApplicationPath; var converter = new PdfConverter(); byte[] pdf = converter.GetPdfBytesFromHtmlString(html, baseUrl); Response.Clear(); Response.ContentType = "application/pdf"; Response.AddHeader("Content-Disposition", "attachment; filename=invoice.pdf"); Response.BinaryWrite(pdf); Response.End(); }
Convert a page by its URL
The converter requests the page like a browser. Pass the cookies when the page needs the visitor's login.
using ExpertPdf.HtmlToPdf; protected void BtnPdf_Click(object sender, EventArgs e) { var converter = new PdfConverter(); // Pass the session cookie if the page needs the visitor's login converter.HttpCookies.Add(".ASPXAUTH", Request.Cookies[".ASPXAUTH"]?.Value); string url = Request.Url.GetLeftPart(UriPartial.Authority) + ResolveUrl("~/Report.aspx"); byte[] pdf = converter.GetPdfBytesFromUrl(url); Response.Clear(); Response.ContentType = "application/pdf"; Response.AddHeader("Content-Disposition", "attachment; filename=report.pdf"); Response.BinaryWrite(pdf); Response.End(); }
In ASP.NET Core
Return the PDF from a Razor Page or a controller. For Razor views rendered to HTML, see HTML to PDF in ASP.NET MVC.
using ExpertPdf.HtmlToPdf; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; public class ExportModel : PageModel { public IActionResult OnGet() { var converter = new PdfConverter(); string url = $"{Request.Scheme}://{Request.Host}/report"; byte[] pdf = converter.GetPdfBytesFromUrl(url); return File(pdf, "application/pdf", "report.pdf"); } }
HTML to PDF in ASP.NET: questions
Is the ASP.NET session available in the page being converted?
Not by itself: the converter requests the page as a new visitor, in a new session. Either render the page to HTML in the current request (with
Server.Execute) and convert the HTML string, or pass the session and authentication cookies to the converter with HttpCookies.Why are the images and CSS missing when I convert an HTML string?
Relative paths cannot be resolved from a string alone. Pass the base URL of the page as the second argument of
GetPdfBytesFromHtmlString, or use full URLs in the HTML.Can the converter open pages that need a login?
Yes: pass a user name and password through
AuthenticationOptions for HTTP authentication, or the session cookies for forms authentication.Which ASP.NET versions are supported?
ASP.NET Web Forms and ASP.NET MVC on .NET Framework (package
ExpertPdfHtmlToPdf), and ASP.NET Core on .NET Core and .NET 5 to .NET 11 (package ExpertPdf.HtmlToPdf.NetCore).Can I try it for free?
Yes. The evaluation is fully functional, has no time limit and includes C# and VB.NET samples for ASP.NET.
Export your ASP.NET pages to PDF.
Download the free evaluation with C# and VB.NET samples for ASP.NET Web Forms, ASP.NET MVC and ASP.NET Core Razor Pages.