HOW TO PRINT A REPORT IN MVC IN PDF FORMATE

Ankit Singh

Total Post:341

Points:2389
Posted by  Ankit Singh
 735  View(s)
Ratings:
Rate this:
I want to  Print a report in mvc in pdf formate
  1. aditya kumar Patel

    Post:254

    Points:1810
    Re: How to Print a report in mvc in pdf formate

    List Page

    Subreport.rdlc

    Mainreport.rdlc

    PDF PRINT OUT

    Code

     $(document).on('click', '.PrintPDF', function (e) {
            var win = window.open('../Home/PrintPDF/"'+ $('#UserIds').val().slice(0,-1)+'"', '_blank');        
            console.log('click');
            if (win) {
                //Browser has allowed it to be opened
                win.focus();
            } else {
                //Broswer has blocked it
                alert('Please allow popups for this site');
            }

        });

     

     

     

      public ActionResult PrintPDF(string id = "27,28,29")
            {
                Session["id"] = id.TrimEnd(',');
                LocalReport localReport = new LocalReport();
                localReport.ReportPath = Server.MapPath("~/User.rdlc");
                ReportDataSource reportDataSource = new ReportDataSource();
                reportDataSource.Name = "UserDataSet";
                Reporting v = new Reporting();
                DataSet dt = new DataSet();
                dt = v.GetReport(Session["id"].ToString());
               //dt = v.GetReport("26");
                reportDataSource.Value = dt.Tables[0];
                localReport.DataSources.Add(reportDataSource);
                localReport.SubreportProcessing += localReport_SubreportProcessing;

     

                string reportType = "PDF";
                string mimeType;
                string encoding;
                string fileNameExtension;
                //The DeviceInfo settings should be changed based on the reportType
                string deviceInfo =
                "<DeviceInfo>" +
                "  <OutputFormat>PDF</OutputFormat>" +
                "  <PageWidth>8.5in</PageWidth>" +
                "  <PageHeight>11in</PageHeight>" +
                "  <MarginTop>0.5in</MarginTop>" +
                "  <MarginLeft>1in</MarginLeft>" +
                "  <MarginRight>1in</MarginRight>" +
                "  <MarginBottom>0.5in</MarginBottom>" +
                "</DeviceInfo>";
                Warning[] warnings;
                string[] streams;
                byte[] renderedBytes;
                //Render the report
                renderedBytes = localReport.Render(
                    reportType,
                    deviceInfo,
                    out mimeType,
                    out encoding,
                    out fileNameExtension,
                    out streams,
                    out warnings);
                //Response.AddHeader("content-disposition", "attachment; filename=Data." + fileNameExtension);         
                Response.ContentType = "application/pdf";
                Response.AddHeader("Content-length", renderedBytes.Length.ToString());
                Response.AddHeader("content-disposition", "inline;filename=Data." + fileNameExtension);
                Response.BinaryWrite(renderedBytes);
                return null;
            }

            private void localReport_SubreportProcessing(object sender, SubreportProcessingEventArgs e)
            {
                if (e.ReportPath.Equals("Subreport"))
                {
                    ReportDataSource reportDataSource = new ReportDataSource();
                    reportDataSource.Name = "DataSet1";
                    Reporting v = new Reporting();
                    DataSet dt = new DataSet();
                    dt = v.GetReport(Session["id"].ToString());
                    reportDataSource.Value = dt.Tables[0];
                    e.DataSources.Add(reportDataSource);
                }
            }

      Modified On Dec-29-2015 10:08:55 PM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!