Razor View to PDF in .Net

Razor View to PDF in .Net

Posted:
Razor View to PDF in .NetPhoto by Glenn Carstens-Peters

Originally Posted On: https://ironpdf.com/docs/questions/razor-view-to-pdf/

 

The following method makes it easy to render a Razor view to a string.

We may then use IronPDF’s HTML to PDF functionality to render that Razor view as a string.

  1. public string RenderRazorViewToString(string viewName, object model)
  2. {
  3. ViewData.Model = model;
  4. using (var sw = new StringWriter())
  5. {
  6. var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext,
  7. viewName);
  8. var viewContext = new ViewContext(ControllerContext, viewResult.View,
  9. ViewData, TempData, sw);
  10. viewResult.View.Render(viewContext, sw);
  11. viewResult.ViewEngine.ReleaseView(ControllerContext, viewResult.View);
  12. return sw.GetStringBuilder().ToString();
  13. }
  14. }

Copy code to clipboardVB C#

Don’t forget to set the optional BaseURI parameter of the IronPdf.HtmlToPdf.RenderHtmlAsPdf Method to load relative assets, CSS, JS and images.

Serving Razor Views as PDFs in ASP.NET MVC

Please read the .NET MVC PDF FAQ to lean how to render a MVC view as a binary PDF file.

Information contained on this page is provided by an independent third-party content provider. Frankly and this Site make no warranties or representations in connection therewith. If you are affiliated with this page and would like it removed please contact pressreleases@franklymedia.com

Powered by Frankly
All content © Copyright 2000 - 2020 WNCONTENT. All Rights Reserved.
For more information on this site, please read our Privacy Policy, and Terms of Service, and Ad Choices.