Create and Save PDF using Rotativa in MVC Application

In today’s article, we will look into converting your HTML page into a PDF using Rotativa also we will look into saving that file in a particular location.

Rotativa is an open-source package available in NuGet Package Manager using which We can easily generate the pdf file. Rotativa uses the web kit engine which is used by Chrome browser to render the HTML. Most of the HTML tags and styles are supported by this framework. The Rotativa framework provides Rotativa namespace. This namespace contains the following classes,

i) ActionAsPdf — This method accepts the action name as a string parameter so that it can be converted into PDF, also we can pass another parameter to it may include route values.

ii) PartialViewAsPdf — Returns partial view as PDF.

iii) UrlAsPdf — It enables us to return any URL as a PDF.

iv) ViewAsPdf — It returns the result as PDF instead of HTML response.

Follow the below steps to use Rotativa library in your MVC project to create PDF files,

Step 1) Create an empty ASP.NET MVC project

Go to visual studio -> Create new project -> Project name -> MVC template -> Create

Step 2) Install Rotativa library in MVC project

Project solution -> Right click on project solution -> Manage NuGet Packages -> Search Rotativa -> Install

Step 3) Writing method for PDF generation using ActionAsPdf

Source Code –

View:

<div style=”text-align:right;padding-top:20px!important;”>

Controller:

public ActionResult About()

Output Screen –

Step 4) Writing method for PDF generation using ActionAsPdf with parameter

Source Code –

View:

<div style=”text-align:right;padding-top:20px!important;”>

Controller:

public ActionResult PrintContactPage()

Output –

Step 5) Writing method for PDF generation using ActionAsPdf and saving that file

Source Code –

View:

<div style=”text-align:right;padding-top:20px!important;”>

Controller:

public ActionResult SaveAboutPage()

Output-

Thank You, See you in the next article !!

You can reach out or connect to me over here,

LinkedIn: https://www.linkedin.com/in/vaibhav-bhapkar

Email: vaibhavbhapkar.medium@gmail.com

If you want some technical topics to be discussed with group of participants please raise a request on following link: http://myblogsenquiry.somee.com/

Technical Speaker | Computer Engineer | Full Stack Web Developer | ML Enthusiast | * Knowledge Shared = Knowledge² *