Skip to footer content

How to Convert Views to PDFs in ASP NET Core MVC

In this tutorial, we explore converting views to PDFs in ASP.NET Core MVC using IronPDF. Start by ensuring IronPDF and its MVC Core extension are installed via the NuGet package manager. The process begins with examining the Person.cs model class, which outlines a Person class with properties like ID, Name, Title, and Description. Next, we delve into the HomeController.cs file, where an asynchronous method creates a list of dummy person data.

For POST requests, the Chrome PDF renderer from IronPDF is utilized to convert the view and model data into a PDF document. The response header is set to display the PDF inline, and the PDF file is returned. For non-POST requests, the view with the list of persons is returned.

In the Persons.cshtml HTML view, we display person details in a table and include a form with a 'Print Person' button triggering the PDF conversion. The _Layout.cshtml file is updated to include a navigation item for easy access to the person's page. The Program.cs file registers essential services like IHttpContextAccessor and the Razor view renderer, crucial for rendering views to PDFs. Running the project, users can navigate to the person's page and convert the view to a PDF using the 'Print Person' button. This feature is invaluable for generating reports or invoices from views.

Further Reading: How to Convert Views to PDFs in ASP.NET Core MVC

Chipego
Software Engineer
Chipego has a natural skill for listening that helps him to comprehend customer issues, and offer intelligent solutions. He joined the Iron Software team in 2023, after studying a Bachelor of Science in Information Technology. IronPDF and IronOCR are the two products Chipego has been focusing on, but his knowledge of all products is growing daily, as he finds new ways to support customers. He enjoys how collaborative life is at Iron Software, with team members from across the company bringing their varied experience to contribute to effective, innovative solutions. When Chipego is away from his desk, he can often be found enjoying a good book or playing football.