Asp.Net MVC how to get view to generate PDF

前端 未结 8 1571
醉酒成梦
醉酒成梦 2020-11-28 19:09

I would like to call an action on a controller. Have the controller get the data from the model. The view then runs and generates a PDF. The only example I have found i

8条回答
  •  离开以前
    2020-11-28 19:54

    A small example using rotativa package in asp.net mvc

    We will create a function to populate the data. We will insert data for 7 days (Feb 1 2018 - Feb 7 2018) showing the first punch and the last punch for particular day with remarks.

    public ReportViewModel PopulateData()
            {
                var attendances = new List
                {
                    new Attendance{ClassName = "present",Day = new DateTime(2018, 02, 01).ToString("ffffd"),Date = new DateTime(2018, 02, 01).ToString("d"),FirstPunch = "09:01:00",LastPunch = "06:00:01",Remarks = ""},
                    new Attendance{ClassName = "absent",Day = new DateTime(2018, 02, 02).ToString("ffffd"),Date = new DateTime(2018, 02, 02).ToString("d"),FirstPunch = "",LastPunch = "",Remarks = "Absent"},
                    new Attendance{ClassName = "holiday",Day = new DateTime(2018, 02, 03).ToString("ffffd"),Date = new DateTime(2018, 02, 03).ToString("d"),FirstPunch = "",LastPunch = "",Remarks = "Democracy Day"},
                    new Attendance{ClassName = "present",Day = new DateTime(2018, 02, 04).ToString("ffffd"),Date = new DateTime(2018, 02, 04).ToString("d"),FirstPunch = "09:05:00",LastPunch = "06:30:01",Remarks = ""},
                    new Attendance{ClassName = "present",Day = new DateTime(2018, 02, 05).ToString("ffffd"),Date = new DateTime(2018, 02, 05).ToString("d"),FirstPunch = "09:01:00",LastPunch = "06:00:01",Remarks = ""},
                    new Attendance{ClassName = "leave",Day = new DateTime(2018, 02, 06).ToString("ffffd"),Date = new DateTime(2018, 02, 06).ToString("d"),FirstPunch = "",LastPunch = "",Remarks = "Sick Leave"},
                    new Attendance{ClassName = "present",Day = new DateTime(2018, 02, 07).ToString("ffffd"),Date = new DateTime(2018, 02, 07).ToString("d"),FirstPunch = "08:35:00",LastPunch = "06:15:01",Remarks = ""}
                };
    
    
                return new ReportViewModel
                {
                    UserInformation = new UserInformation
                    {
                        FullName = "Ritesh Man Chitrakar",
                        Department = "Information Science"
                    },
                    StartDate = new DateTime(2018, 02, 01),
                    EndDate = new DateTime(2018, 02, 07),
                    AttendanceData = attendances
                };
            }
    

    We will then create a function to DownloadPdf. To download the pdf we will need to create 2 function. 1. to download pdf 2. to view pdf

    public ActionResult DownloadPdf()
            {
                var filename = "attendance.pdf";
    
                /*get the current login cookie*/
                var cookies = Request.Cookies.AllKeys.ToDictionary(k => k, k => Request.Cookies[k]?.Value);
    
                return new ActionAsPdf("PdfView", new
                {
                    startDate = Convert.ToDateTime(Request["StartDate"]),
                    endDate = Convert.ToDateTime(Request["EndDate"])
                })
                {
                    FileName = filename,
                    /*pass the retrieved cookie inside the cookie option*/
                    RotativaOptions = {Cookies = cookies}
                };
            }
    
    public ActionResult PdfView()
            {
                var reportAttendanceData = PopulateData();
    
                return View(reportAttendanceData);
            }
    

    You can view the detail explanation on this link . Visit here .

    Curtesoy : thelearninguy.com

提交回复
热议问题