This article explains how to create a PDF in Razor View using RazorPDF in MVC. RazorDPF is a package that uses iTextSharp internally.
We will create a simple marks card example to see the RazorPDF package. Let's create a ASP.NET MVC 4 project, as shown in Figure 1.1.
Select "ASP.NET MVC 4 Web Application" and click on the "OK" button. This brings up the dialog box to select the MVC templates.
Figure 1.1
Select Internet Application template and ensure that Razor is selected as the view engine, as shown in Figure 1.2.
Figure 1.2
Install RazorPdf using NuGet Package Manager
Right-click on the References node in Solution Explorer, as shown in Figure 1.3. Click on "Manage NuGet Packages". This brings up the NuGet Manage Package dialog box, that is used to install the RazorPDF package.
The NuGet dialog box lists installed packages by default, so click on the online tab, as shown in Figure 1.4.
Figure 1.4
Type "RazorPDF" in the search bar in the top-right side of NuGet. Once you find the RazorPDF, click on the install button to install the RazorPDF package in your project. Once installation is finished, automatically iTextSharp and RazorPDF will be added as a reference in your project, as shown in Figure 1.5.
Figure 1.5
Create a Marks Card Model
Create a class "MarksCard" to model the marks card.
- namespace RazorPDFDemo.Models
- {
- public classMarksCard
- {
- public int RollNo
- {
- get;
- set;
- }
- public string Subject
- {
- get;
- set;
- }
- public int FullMarks
- {
- get;
- set;
- }
- public int Obtained { get; set; }
- }
- }
Once you have created the MarksCard class, build your project. So we can create the strongly typed view.
Create Controller for StudentMarks
Right-click on the Controllers folder then select "Add" -> "Controller", as shown in Figure 1.6.
Figure 1.6
The Controller dialog box will open, as shown in Figure 1.7. Change the controller name to Students and select "Empty API controller" in the Scaffolding template and click on the "Add" button.
Figure 1.7
We will create a list and store some data in the list. See the following code:
- using System.Collections.Generic;
- using System.Web.Mvc;
- using RazorPDFDemo.Models;
- namespace RazorPDFDemo.Controllers
- {
- public class StudentsController : Controller
- {
-
- public ActionResult Index()
- {
- var studentMarks = new List<MarksCard>()
- {
- new MarksCard()
- {
- RollNo = 101, Subject = "C#",
- FullMarks = 100, Obtained = 90},new MarksCard() {RollNo = 101, Subject = "asp.net", FullMarks = 100, Obtained = 80},new MarksCard() {RollNo = 101, Subject = "MVC", FullMarks = 100,
- Obtained = 100},new MarksCard() {RollNo = 101, Subject = "SQL Server", FullMarks = 100, Obtained = 75},
- };
- return new RazorPDF.PdfResult(studentMarks, "Index");
- }
- }
- }
- }
Now our controller is ready. We need to create a view to display this marks card. Right-click on the Index() controller and click on "Add View". A view dialog box will be displayed, as shown in Figure 1.8. Make sure that Razor is selected in the view engine.
Select MarksCard in the Model class and click on the "Add" button.

Figure 1.8
The view will be created for the marks card. I have done some cosmetic changes in the view to make the look and feel like a marks card, see the following view code:
- @model IEnumerable
- <RazorPDFDemo.Models.MarksCard>
- @{Layout = null;
- }
- <!DOCTYPE html>
- <html>
- <body>
- <table border="1" width='500' bordercolor="RED">
- <tr>
- <td colspan="3" bgcolor="LightGreen" align="center" valign="top">SSLC Marks Sheet 2013</td>
- </tr>
- <tr>
- <td>@{ var rollNumber = Model.Select(z => z.RollNo).Take(1).ToArray();}Riyaz Akhter<br />RollNo:@rollNumber[0]</td>
- <td colspan="2" align="left"><img src="@Server.MapPath("~/Images/images.jpg")" width="100" height="100"/></td>
- </tr>
- <tr>
- <td bgcolor="lightblue">@Html.DisplayNameFor(moel => moel.Subject)</td>
- <td bgcolor="lightblue">@Html.DisplayNameFor(model => model.FullMarks)</td>
- <td bgcolor="lightblue">@Html.DisplayNameFor(model => model.Obtained)</td>
- </tr>
- @{int total = 0;}@foreach (var item in Model){
- <tr>
- <td>@Html.DisplayFor(modelItem => item.Subject)</td>
- <td>@Html.DisplayFor(modelItem => item.FullMarks)</td>
- <td>@Html.DisplayFor(modelItem => item.Obtained)</td>
- </tr>
- total += item.Obtained;}
- <tr>
- <td></td>
- <td><strong><font color="GREEN">Total</font></strong></td>
- <td>@total</td>
- </tr>
- </table>
- </body>
- </html>
In the code above, I have changed the default layout to null and also I have added <html> and <body> tags.
Now your application is ready to run. Once you run the application it will look as in Figure 1.9.
Figure 1.9
Now we will convert this view to PDF. Go to your student controller and make a small change in your controller, as shown in Figure 1.10.
Figure 1.10
Yes, that's it. pdfResult is a method in the RazorPDF class that takes a model object and view name as a parameter or you can pass a model object without an action name if your action name is a PDF.
Now run your application. This time the PDF will be displayed instead of your view, as shown in Figure 1.11.
Figure 1.11
It is very easy to create a PDF using RazorPDF in ASP.NET MVC.
Ilya SadukovskiyPosted Jun 11, 2022, 2:09 PM
Does not seem to work with .net core 5 :( PdfResult cannot be converted to MVC Core ActionResult
Former memberPosted Mar 15, 2016, 10:16 AM
Hi, My in Error "RazorPDF.PdfResult
Sujay AnandPosted Apr 20, 2015, 8:53 AM
As I m getting this error Could not load type 'iTextSharp.text.html.HtmlParser' from assembly 'itextsharp, Version=5.0.5.0, Culture=neutral, PublicKeyToken=8354ae6d2174ddca'. How to resolve this error?
Alok KumarPosted Sep 10, 2014, 4:51 AM
tell me solution please
Alok KumarPosted Sep 10, 2014, 4:51 AM
why unnecessary spce in first column..
Alok KumarPosted Sep 9, 2014, 1:35 AM
please help
Alok KumarPosted Sep 9, 2014, 1:34 AM
%PDF-1.4 %???? 2 0 obj <>stream x?+?r ?26S?00SI?2P?53?? -B?4?Hzu???L?5????G?l[??P???7?47???p??kW D?I? endstream endobj 4 0 obj<>/ProcSet [/PDF /Text /ImageB /ImageC /ImageI]>>>> endobj 1 0 obj<> endobj 3 0 obj<> endobj 5 0 obj<> endobj 6 0 obj<> endobj xref 0 7 0000000000 65535 f 0000000376 00000 n 0000000015 00000 n 0000000463 00000 n 0000000220 00000 n 0000000513 00000 n 0000000557 00000 n trailer <<21969ad93144c0afad51f859359c0e6c>]/Root 5 0 R>> startxref 700 %%EOF getting this format
alexi espniozaPosted Aug 27, 2014, 4:29 PM
I have a view with javascript embebed... what can I do ?
amit parmarPosted Jul 29, 2014, 9:09 AM
not working, please do not put this kind of code
Gift MokoenaPosted Jul 9, 2014, 3:47 AM
I have problem Could not load type 'iTextSharp.text.html.HtmlParser' from assembly 'itextsharp, Version=5.5.1.0, Culture=neutral, PublicKeyToken=8354ae6d2174ddca'.
Gift MokoenaPosted Jul 8, 2014, 9:59 AM
great I will test before the end of today hope I get helped
Naeem AhmedPosted May 3, 2014, 12:06 PM
Great Article. Was able to export in 10min. Thanks :)
Abhishek GuptaPosted Apr 28, 2014, 6:49 AM
pdf is coming blank
AnjaliPosted Apr 10, 2014, 2:53 AM
not working in mvc application,using the same code
Balaji SelvarajanPosted Jan 3, 2014, 10:11 AM
Good Article
WaynePosted Aug 16, 2013, 11:56 AM
Thanks. This is very helpful! BTW, how can I save the PdfResult to a PDF file?