Home > DeveloperSection > Forums > How to Print a report in mvc in pdf formate
Ankit Singh

Total Post:341

Points:2389
Posted on    December-29-2015 9:54 PM

 .NET Reporting  PDF 
Ratings:


 1 Reply(s)
 289  View(s)
Rate this:
I want to  Print a report in mvc in pdf formate


aditya kumar Patel

Total Post:242

Points:1722
Posted on    December-29-2015 10:02 PM

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

Don't want to miss updates? Please click the below button!

Follow MindStick