ASP.NET MVC guideCore · MVC 5
HTML to PDF in ASP.NET MVC: Razor views to PDF
Design the document as a normal Razor view (.cshtml), with your layout, model and CSS. Render it to an HTML string, convert the string with the ExpertPdf HTML to PDF Converter and return the PDF from the controller.
ASP.NET Core MVC
A small view renderer turns the Razor view into HTML; the controller converts it and returns the file.
using ExpertPdf.HtmlToPdf; using Microsoft.AspNetCore.Mvc; public class InvoiceController : Controller { private readonly IViewRenderer _views; public InvoiceController(IViewRenderer views) => _views = views; public async Task<IActionResult> Pdf(int id) { var model = LoadInvoice(id); string html = await _views.RenderAsync(ControllerContext, "Invoice", model); var converter = new PdfConverter(); string baseUrl = $"{Request.Scheme}://{Request.Host}/"; byte[] pdf = converter.GetPdfBytesFromHtmlString(html, baseUrl); return File(pdf, "application/pdf", $"invoice-{id}.pdf"); } }
ASP.NET MVC 5
On .NET Framework the view engine renders the view to a StringWriter.
using System.IO; using System.Web.Mvc; using ExpertPdf.HtmlToPdf; public class InvoiceController : Controller { public ActionResult Pdf(int id) { string html = RenderViewToString("Invoice", LoadInvoice(id)); var converter = new PdfConverter(); string baseUrl = Request.Url.GetLeftPart(UriPartial.Authority) + Url.Content("~/"); byte[] pdf = converter.GetPdfBytesFromHtmlString(html, baseUrl); return File(pdf, "application/pdf", "invoice-" + id + ".pdf"); } private string RenderViewToString(string viewName, object model) { ViewData.Model = model; using (var writer = new StringWriter()) { var view = ViewEngines.Engines.FindView(ControllerContext, viewName, null); var context = new ViewContext(ControllerContext, view.View, ViewData, TempData, writer); view.View.Render(context, writer); view.ViewEngine.ReleaseView(ControllerContext, view.View); return writer.ToString(); } } }
Or convert a page by its URL
For public pages, pass the address to GetPdfBytesFromUrl.
[HttpPost] public ActionResult Convert(string url) { var converter = new PdfConverter(); byte[] pdf = converter.GetPdfBytesFromUrl(url); return File(pdf, "application/pdf", "RenderedPage.pdf"); }
HTML to PDF in ASP.NET MVC: questions
Why render the view to a string instead of converting its URL?
Rendering in the current request keeps the user's session, authentication and model, and avoids a second request to your own site. Converting by URL works too, for public pages or with the session cookies passed to the converter.
Why do the images and CSS of the view not appear?
The view's HTML uses paths relative to your site. Pass the site's base URL as the second argument of
GetPdfBytesFromHtmlString.How do I control page breaks in the view?
With CSS in the view:
page-break-before: always, page-break-after: always and page-break-inside: avoid. The converter also avoids cutting lines of text and images by itself.Are there ASP.NET MVC samples?
Yes. The download includes full ASP.NET MVC samples in C# and VB.NET, and ASP.NET Core Razor Pages samples in C#.
Turn your Razor views into PDF.
Download the free evaluation with ASP.NET MVC samples in C# and VB.NET and ASP.NET Core Razor Pages samples.