Tuesday, August 8, 2017

Working with PDF IN ASP.NET MVC with Entity FrameWork.

With this example you can create pdf and print pdf.

If You are using ASP.NET MVC , PDF can be created by using Rotativa 

Rotativa is a framework that is used to print PDF documents in ASP.NET MVC Applications. This framework we can download using the nuget packages as shown below


Go to Visual Studio

File-> New-Project



The below screen gets displayed. Give any name of the application.


After clicking on ok , the below screen gets displayed: 
Select Empty and select the check box MVC




Let us configure rotative using Nuget as shown below .
Goto to Visual Studio Tools-NuGet Package Manager->Package Manager Console
The below screen gets displayed-> Type the Nuget package command for Rotative




Go to App Data to Add the DataBase as shown below
App Data--->Add--> New Item--> Sql Server DataBase 


In the visual Studio in the solution explorer you can find as shown below

Click on the Open,  the below screen gets displayed  and select New Query and execute the below script  to create the table





Script: 

CREATE TABLE [dbo].[EmployeeInfo] (
    [EmpNo]       INT          IDENTITY (1, 1) NOT NULL,
    [EmpName]     VARCHAR (50) NOT NULL,
    [Salary]      INT          NOT NULL,
    [DeptName]    VARCHAR (50) NOT NULL,
    [Designation] VARCHAR (50) NOT NULL,
    [HRA]         AS           ([Salary]*(0.2)),
    [TA]          AS           ([Salary]*(0.15)),
)

Now the table has been created , 

GoTo Model Folder
Add-->New Item-->ADO.NET Entity Data Model




Give any Name and click on Add, the below screen will be displayed







And Click On Finish

Now goto Contoller Folder and Add New Item


Controller Code:

public class MVCPDFController : Controller
    {
        public ActionResult Index()
        {
            var emps = ctx.EmployeeInfoes.ToList();
            return View(emps);
        }
        EmployeeModel ctx;
        public MVCPDFController()
        {
            ctx = new Models.EmployeeModel();
        }
        public ActionResult PrintAllReports()
        {
            var report = new ActionAsPdf("Index");
            return report;
        }
        public ActionResult PrintPdfDetails(int id)
        {
            var emp = ctx.EmployeeInfoes.Where(e => e.EmpNo == id).First();
            return View(emp);
        }
        public ActionResult PrintSalaryDetails(int id)
        {
            var report = new ActionAsPdf("PrintPdfDetails", new { id = id });
            return report;
        }
    }


Index.cs.html


@model IEnumerable<WebApplication1.Models.EmployeeInfo>
@{
    ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
    @Html.ActionLink("View In PDF Format", "PrintAllReports")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.EmpName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Salary)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.DeptName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Designation)
        </th>
        <th></th>
    </tr>
    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.EmpName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Salary)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.DeptName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Designation)
            </td>
           
            <td>
                @Html.ActionLink("Print Pay Slip", "PrintSalaryDetails", new { id = item.EmpNo })
            </td>
        </tr>
    }
</table>



PrintPDFDetails.cs.html

@model WebApplication1.Models.EmployeeInfo

@{
    ViewBag.Title = "PrintPdfDetails";
}

<div>
<h2> Salary Details of the Employee : @Model.EmpName 
</h2>
<table>
    <tr>
        <td>Department Name :</td>
        <td>@Model.DeptName</td>
    </tr>
    <tr>
        <td>Designation :</td>
        <td>@Model.Designation</td>
    </tr>
</table>
    <table>
        <tr>
            <td>
                <table>
                    <tr>
                        <td colspan="2">Income Details</td>
                    </tr>
                    <tr>
                        <td>Salary</td>
                        <td>@Model.Salary</td>
                    </tr>
                    <tr>
                        <td>HRA</td>
                        <td>@Model.HRA</td>
                    </tr>
                    <tr>
                        <td>TA</td>
                        <td>@Model.TA</td>

                    </tr>
                </table>
            </td>
        </tr>
    </table>

    <p>
        @Html.ActionLink("Back to Home", "Index")
    </p>
</div>

OutPut


On Clicking on Print Pay Slip



On clicking on View pdf




1 comment:

Thank you for visiting my blog

Kubernetes

Prerequisites We assume anyone who wants to understand Kubernetes should have an understating of how the Docker works, how the Docker images...