Click here to Skip to main content
15,888,293 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
<pre lang="c#">
public class ReportsResult : ActionResult
    {
        public ReportsResult(byte[] data, string mineType)
        {
            this.Data = data;
            this.MineType = mineType;
        }

        public byte[] Data { get; set; }
        public string MineType { get; set; }

        public override void ExecuteResult(ControllerContext context)
        {
            if (Data == null)
            {
                new EmptyResult().ExecuteResult(context);
                return;
            }
            context.HttpContext.Response.ContentType = MineType;

            using (MemoryStream ms = new MemoryStream(Data))
            {
                ms.Position = 0;
                using (StreamReader sr = new StreamReader(ms))
                {
                    context.HttpContext.Response.Output.Write(sr.ReadToEnd());
                }
            }
        }
    }

C#
public static class HotelReport
   {
       public static byte[] GenerateReport(string dtatSetName, IEnumerable dataSource, string reportFilePath,
                              string reportType, out string mimeType, out string encoding, out string fileNameExtension)
       {

           ReportDataSource reportDataSource = new ReportDataSource(dtatSetName, dataSource);

           LocalReport localReport = new LocalReport();
           localReport.ReportPath = reportFilePath;
           localReport.DataSources.Add(reportDataSource);

           string deviceInfo =
               "<DeviceInfo>" +
               "  <OutputFormat>" + reportType + "</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;

           renderedBytes = localReport.Render(
               reportType,
               deviceInfo,
               out mimeType,
               out encoding,
               out fileNameExtension,
               out streams,
               out warnings);

           return renderedBytes;
       }
   }

C#
public ActionResult EmployeesNumberPerYear()
        {
         string dtatSetName = "DsENPerYear";
         var dataSource = 
                 EmployeeReports.EmployeesNumberPerYear(employeeRepository);
         string reportFilePath = 
                 Server.MapPath("~/RDLC/Employee/EmployeesNumberPerYear.rdlc");
         string reportType = "PDF";

         string mimeType;
         string encoding;
         string fileNameExtension;

         byte[] renderedBytes = 
            HotelReport.GenerateReport(dtatSetName, dataSource, reportFilePath,
                reportType, out mimeType, out encoding, out fileNameExtension);

        return new ReportsResult(renderedBytes, mimeType);
        }

<pre lang="HTML">
<div>
    @{
        Html.RenderAction("EmployeesNumberPerYear", "EmployeeReports");
    }
</div>
Posted
Updated 31-Mar-12 16:07pm
v3
Comments
Mehul M Thakkar 17-Jan-14 4:26am    
so what is the question? It loads junk in browser.

1 solution

If EmployeeReports is your controller, then I'd create a new method like this:

C#
public void EmployeesNumberPerYearToPDF()
        {
         string dtatSetName = "DsENPerYear";
         var dataSource = 
                 EmployeeReports.EmployeesNumberPerYear(employeeRepository);
         string reportFilePath = 
                 Server.MapPath("~/RDLC/Employee/EmployeesNumberPerYear.rdlc");
         string reportType = "PDF";
 
         string mimeType;
         string encoding;
         string fileNameExtension;
 
         byte[] renderedBytes = 
            HotelReport.GenerateReport(dtatSetName, dataSource, reportFilePath,
                reportType, out mimeType, out encoding, out fileNameExtension);
 
        System.Web.HttpContext.Current.Response.ContentType = mimeType;
        System.Web.HttpContext.Current.Response.BinaryWrite(renderedBytes);

        }


Then you could embed Google Doc Viewer (an iframe) in your div and specify the PDF file you want to display i.e. in your case pass the URL to your controller that will return the byte array of the PDF file. This is the code you should add inside your div:

HTML
<iframe src="http://docs.google.com/gview?url=@Url.Action("EmployeesNumberPerYearToPDF", "EmployeeReports", null, Request.Url.Scheme)&embedded=true" style="width:600px; height:500px;" frameborder="0"></iframe>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900