In this article, we are going to learn how to use Rotativa.AspNetCore tool to create PDFs from Views in ASP.NET Core. If you have worked with ASP.NET MVC, the Rotativa tool is already available there, which we can use for generating pdfs.

Icons made by Freepik from www.flaticon.com is licensed by CC 3.0 BY
This tool was created by Italian Giorgio Bozio. He had a requirement for generating pdf in ASP.NET MVC and a recurring task was to set up a way to create PDF docs, for business flow or reporting.
In the past, I used a lot of Crystal Reports. It had a very nice editor that users who weren't programmers could use with ease. Sometimes, though, it was problematic when installing on new servers and it was expensive, especially for projects where the requirements did not involve producing new layouts frequently.
Then, I found the wkhtmltopdf command line tool, tried it, and looked at opinions on the web. The first experiences were great. It was fast and the PDF docs came out pretty nice. And, most importantly, it was free.
History of Rotativa referred from - http://letsfollowtheyellowbrickroad.blogspot.in/
What is wkhtmltopdf (wkhtmltopdf.exe)?
wkhtmltopdf is a C library.
wkhtmltopdf and wkhtmltoimage are open source (LGPLv3) command line tools to render HTML into PDF and various image formats using the Qt Web Kit rendering engine.
Definition referred to- https://wkhtmltopdf.org/
Tool to use with ASP.NET MVC
If you want a tool for creating pdf in ASP.NET MVC then you can download it from NuGet.
Command: - Install-Package Rotativa -Version 1.7.4-rc
Do not use 1.7.3, it has issues.
Find it on GitHub:- https://github.com/webgio/Rotativa
Prerequisite
- Visual Studio 2017 with ASP.NET CORE 2.0
- AspNetCore 1.0.6 version
Creating ASP.NET Core MVC Application
Now let’s start with creating a simple application in ASP.NET MVC Core.
Open New Visual Studio 2017 IDE.
After opening IDE, next, we are going to create ASP.NET MVC Core project. For doing that, just click File - New - Project.
After choosing a project, a new dialog will pop up with the name "New Project". In that, we are going to choose Visual C# Project Templatesà.NET Coreà ASP.NET Core Web Application. Then, we are going to name the project as "DemoRotativa".

After configuring the project, next, we are going to see project structure.

After creating the project next, we are going to install Rotativa.AspNetCore to the project.
Installing Rotativa.AspNetCore to project
We are going to install Rotativa.AspNetCore 1.0.6 version which needs dependency of AspNetCore MVC 2.o. We already have created ASP.NET CORE 2.0 project; we do not need to worry.
We can install it via package manager console or via Manage package solution Windows.

Let’s install it using package manager console.

After installing Rotativa.AspNetCore next, we need to do configuration.
Configuration Rotativa.AspNetCore setting in a startup.cs class
We add this setting in a startup.cs class inside Configure method to set a relative path to the wkhtmltopdf.exe file to access.
RotativaConfiguration.Setup(env);

Next, we need to add a new Folder in WWW with name “Rotativa” and inside this folder, we are going to add
wkhtmltopdf.exe, wkhtmltoimage.exe files.
You can download this files from GitHub if you download sample solution which I have provided
https://github.com/webgio/Rotativa.AspNetCore
Note
Download the solution and you will get this exe files.

Next, after adding exe files, we are going to add Controller.
Adding DemoController
For adding controller just right click on controller folder then choose "Add," inside that, choose to Add New item. A new dialog will popup with Add New item, inside that choose "MVC Controller Class" and Name your controller as "DemoController" and click on Add button to create a Demo controller.

After adding controller, next, we are going to Add Action to this Controller.
Adding [HttpGet] DemoViewAsPDF Action Method
We are going to add action method with name DemoViewAsPDF and this action method will return ViewAsPdf result.
Complete Code Snippet of DemoViewAsPDF
- using Microsoft.AspNetCore.Mvc;
- using Rotativa.AspNetCore;
- namespace DemoRotativa.Controllers
- {
- public class DemoController : Controller
- {
- public IActionResult DemoViewAsPDF()
- {
- return new ViewAsPdf("DemoViewAsPDF");
- }
- }
- }
After adding Action Method, next, we are going to add “DemoViewAsPDF” View.
Adding DemoViewAsPDF View

On this View, we have added simple Billing Invoice.
Code snippet of DemoViewAsPDF View
- @{
- Layout = null;
- }
- <html>
- <head>
- <meta charset="utf-8">
- <title>Demo</title>
- <link rel="stylesheet" href="style.css">
- <link rel="license" href="https://www.opensource.org/licenses/mit-license/">
- <script src="script.js"></script>
- <link href="~/css/demo.css" rel="stylesheet" />
- </head>
- <body>
- <header>
- <h1>Invoice</h1>
- <address contenteditable>
- <p>Jonathan Neal</p>
- <p>101 E. Chapman Ave<br>Orange, CA 92866</p>
- <p>(800) 555-1234</p>
- </address>
- <span><img src="D:\DemoRotativa\DemoRotativa\DemoRotativa\wwwroot\images\Invoice.jpg" /><input type="file" accept="image/*"></span>
- </header>
- <article>
- <h1>Recipient</h1>
- <address contenteditable>
- <p>Demo Company<br>c/o Saineshwar</p>
- </address>
- <table class="meta">
- <tr>
- <th><span contenteditable>Invoice #</span></th>
- <td><span contenteditable>000001</span></td>
- </tr>
- <tr>
- <th><span contenteditable>Date</span></th>
- <td><span contenteditable>January 1, 2018</span></td>
- </tr>
- <tr>
- <th><span contenteditable>Amount Due</span></th>
- <td><span id="prefix" contenteditable>र </span><span>600.00</span></td>
- </tr>
- </table>
- <table class="inventory">
- <thead>
- <tr>
- <th><span contenteditable>Item</span></th>
- <th><span contenteditable>Description</span></th>
- <th><span contenteditable>Rate</span></th>
- <th><span contenteditable>Quantity</span></th>
- <th><span contenteditable>Price</span></th>
- </tr>
- </thead>
- <tbody>
- <tr>
- <td><a class="cut">-</a><span contenteditable>Front End Consultation</span></td>
- <td><span contenteditable>Experience Review</span></td>
- <td><span data-prefix>र </span><span contenteditable>150.00</span></td>
- <td><span contenteditable>4</span></td>
- <td><span data-prefix>र </span><span>600.00</span></td>
- </tr>
- </tbody>
- </table>
- <table class="balance">
- <tr>
- <th><span contenteditable>Total</span></th>
- <td><span data-prefix>र </span><span>600.00</span></td>
- </tr>
- <tr>
- <th><span contenteditable>Amount Paid</span></th>
- <td><span data-prefix>र </span><span contenteditable>0.00</span></td>
- </tr>
- <tr>
- <th><span contenteditable>Balance Due</span></th>
- <td><span data-prefix>र </span><span>600.00</span></td>
- </tr>
- </table>
- </article>
- <aside>
- <h1><span contenteditable>Additional Notes</span></h1>
- <div contenteditable>
- <p>A finance charge of 1.5% will be made on unpaid balances after 30 days.</p>
- </div>
- </aside>
- </body>
- </html>
Now we have completed with all settings so let’s save this application and access your First Pdf.
URL to access:- http://localhost:#####/demo/DemoViewAsPDF
Snapshot of Pdf
Setting Margin to Pdf
For displaying a demo of Margin we are going to add another Action Method with name “DemoPageMarginsPDF” and along with this we are also going to add View with the same name “DemoPageMarginsPDF”.






Abdul AkherPosted Jan 19, 2021, 10:10 AM
I am using asp.net core 2.0, My view content model data along with google chart and other images, this Rotativa.AspNetCore (v1.1.1) component only rendering model data. PDF does not show any chart or images. any help please? Thanks a lot.
Maulik MananiPosted Nov 25, 2020, 5:58 PM
I can not host exe in my shared hosting. Now what to do?
Deeksha SinghPosted May 28, 2020, 2:18 PM
Sir how to download it instead of open view as pdf
Karan EkkawalaPosted Aug 8, 2019, 1:10 AM
I would like to ask a question that if we deployed it on the azure, will it work or any kind of dependency, also where we need to place the .exe file ?
yesubabu kPosted Jun 27, 2019, 1:11 AM
Super boss
amit kumarPosted May 17, 2019, 6:55 AM
If i have to print multiple invoices in one page then ok. but what to do when i have to print multiple invoices separately then what to do...?
Muhammad khanPosted Mar 20, 2019, 6:33 AM
Remove some columns that are visible in the Grid, so that they do not appear in the exported pdf, please help me
Jahan AlemPosted Feb 28, 2019, 3:20 AM
Hi, How can I set the path of cshtml file? for example I want to show contain of ViewComponent and it located in \\Views\MyController\Components\MyViewComponent\Default.cshtml
Linz KrishnaPosted Jan 31, 2019, 6:13 AM
Great job. Appreciated your effort. I would like to have the source code to get both style and demo css since your source code drop box has been turned off. Please send by email if you don't mind.
Jerome CPosted Nov 30, 2018, 8:39 AM
If it possible to pie chart image show using rotativa.
Jerome CPosted Nov 30, 2018, 8:38 AM
Your article nice i have problem with rotativa using pie chart image,
SUJIT PATILPosted May 8, 2018, 12:50 AM
How can i generate multiple pages with same structure but dynamic data?
Marco MedinaPosted Apr 13, 2018, 12:22 PM
What about outputting html saved from a db, It's not converting html in pdf when I run it.
Chris HouserPosted Apr 4, 2018, 8:07 AM
How would you save the PDF, or send it as an attachment? I've tried to convert it to byte[] but I'm getting an Implicit conversion error: Cannot convert System.Threading.Tasks.Task<byte[]> to byte[]. Thank you and great article!
sn gShadowPosted Mar 4, 2018, 4:29 PM
That's a Good article :)
Sagar Pandurang KapPosted Feb 20, 2018, 11:07 PM
Thats amazing.Useful..