Create and Save PDF using Rotativa in MVC Application

Vaibhav Bhapkar
3 min readDec 26, 2020

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;”>@Html.ActionLink(“Print About Page”,”PrintAboutPage”)</div><div><h2>@ViewBag.Title.</h2><h3>@ViewBag.Message</h3><p>Use this area to provide additional information.</p></div>

Controller:

public ActionResult About(){ViewBag.Message = “Your application description page.”;return View();}public ActionResult PrintAboutPage(){var report = new Rotativa.ActionAsPdf(“About”);return report;}

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;”>@Html.ActionLink(“Print Contact Page”, “PrintContactPage”)</div><div><h2>@ViewBag.Title.</h2><h3>@ViewBag.Message</h3></div>

Controller:

public ActionResult PrintContactPage(){var report = new Rotativa.ActionAsPdf(“Contact”, new { name = “Vaibhav” });return report;}

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;”>@Html.ActionLink(“Print About Page”,”PrintAboutPage”)</div><div style=”text-align:right;padding-top:20px!important;”>@Html.ActionLink(“Save About Page”, “SaveAboutPage”)</div><div><h2>@ViewBag.Title.</h2><h3>@ViewBag.Message</h3><p>Use this area to provide additional information.</p></div>

Controller:

public ActionResult SaveAboutPage(){string fileName = “Test.pdf”;string fullPath = @”C:\Users\Vaibhav\Documents\RotativaPdf\” + fileName;var report = new Rotativa.ActionAsPdf(“About”){PageOrientation = Rotativa.Options.Orientation.Portrait,PageSize = Rotativa.Options.Size.A4,PageMargins = new Margins(0, 0, 0, 0),};if (!System.IO.File.Exists(fullPath)){var byteArray = report.BuildPdf(ControllerContext);var fileStream = new FileStream(fullPath, FileMode.Create, FileAccess.Write);fileStream.Write(byteArray, 0, byteArray.Length);fileStream.Close();}return report;}

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/

--

--

Vaibhav Bhapkar

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