locked
Convert HTML into PDF using Rotativa in MVC RRS feed

  • Question

  • User-1539946879 posted

    I want to convert HTML into PDF using Rotativa.

    I have tried this:

    public GridPagination FilterData(int? PageNumber, StaffInfoViewModel filters)
    {
    GridPagination gridData = new GridPagination();
    double count = 0;
    try
    {
    // Getting all the Data from Database
    md.StaffList = _repository.StaffList();
    // Checking if the Page number is passed and is greater than 0 else considered as 1
    gridData.CurrentPage = PageNumber.HasValue ? PageNumber.Value <= 0 ? 1 : PageNumber.Value : 1;
    // Assigning the list of data to the Model's property
    gridData.StaffInfoData = md.StaffList;
    // Assigning filters
    gridData.StaffFilters = filters;

    //Getting the List with the matching emp_no
    if (filters.Id>0)
    {
    gridData.StaffInfoData = gridData.StaffInfoData.Where(x => x.Id.ToString().Contains(filters.Id.ToString())).ToList();
    }

    //Getting the List with the matching first_name
    if (!string.IsNullOrEmpty(filters.Name))
    {
    gridData.StaffInfoData = gridData.StaffInfoData.Where(x => x.Name.ToLower().Contains(filters.Name.ToLower())).ToList();
    }

    //Getting the List with the matching last_name
    if (!string.IsNullOrEmpty(filters.Designation))
    {
    gridData.StaffInfoData = gridData.StaffInfoData.Where(x => x.Designation.ToLower().Contains(filters.Designation.ToLower())).ToList();
    }

    //Getting the List with the matching gender
    if (!string.IsNullOrEmpty(filters.Gender))
    {
    gridData.StaffInfoData = gridData.StaffInfoData.Where(x => x.Gender.ToLower().Contains(filters.Gender.ToLower())).ToList();
    }

    // If there are multiple filter key passed then the above condition will work as an operator condition

    // Total data count after filter
    gridData.TotalData = gridData.StaffInfoData.Count();

    // Getting the total pages
    count = (double)gridData.TotalData / gridData.TakeCount;
    gridData.TotalPage = (int)Math.Ceiling(count);

    // assigning the filtered data to model
    // This is the formula for skiping the previous page's data and taking the current page's
    gridData.StaffInfoData = gridData.StaffInfoData.Skip((gridData.CurrentPage - 1) * gridData.TakeCount).Take(gridData.TakeCount).ToList();
    }
    catch (Exception ex)
    {
    gridData = new GridPagination();
    }
    //returning the Grid.
    return gridData;
    }

    public ActionResult Index()
    {
    return View(FilterData(1, new StaffInfoViewModel()));
    }

    [HttpPost]
    public ActionResult PaginateData(int pageNo, StaffInfoViewModel StaffFilters)
    {
    return PartialView("_StaffList", FilterData(pageNo, StaffFilters));
    }

    public ActionResult DownloadViewPDF()
    {
    return new ActionAsPdf("Index");
    }

    By using above code, Only page no. 1 is converted, how can I convert another pages?

    Tuesday, November 27, 2018 9:56 AM

All replies

  • User-271186128 posted

    Hi Riju Maharjan,

    I want to convert HTML into PDF using Rotativa.

    You could add a temp page to show all of the results (without implementing paging), then convert this page to pdf.

    Please refer to the following article to learn how to use Rotativa.

    Generate PDF in ASP.NET MVC Using Rotativa 

    Best regards,
    Dillion

    Wednesday, November 28, 2018 3:03 AM
  • User-1539946879 posted

    Thank you...

    Wednesday, November 28, 2018 3:37 AM