How to render client report definition files (.rdlc) directly to the Response stream without preview

 

Note: I cover this technique in a more recent post here : Rendering an RDLC directly to the Response stream in ASP.NET MVC

 

A ReportViewer control is normally used to open a report definition file, process it and load it into the viewing area.

The simple method below allows you to render the report directly to the response stream without using the ReportViewer control. This might be useful in cases where you want to render a non interactive report.

The example below renders the report in PDF format. The other report types available when using the LocalReport.Render method are β€œExcel”and β€œImage”.

 

 

/// <summary>

/// References:

/// </summary>

private void RenderReport() {

    LocalReport localReport = new LocalReport();

    localReport.ReportPath = Server.MapPath("~/Report.rdlc");
  

    //A method that returns a collection for our report

    //Note: A report can have multiple data sources

    List<Employee> employeeCollection = GetData();

 

    //Give the collection a name (EmployeeCollection) so that we can reference it in our report designer

    ReportDataSource reportDataSource = new ReportDataSource("EmployeeCollection", employeeCollection);

    localReport.DataSources.Add(reportDataSource);

 

    string reportType = "PDF";

    string mimeType;

    string encoding;

    string fileNameExtension;

 

    //The DeviceInfo settings should be changed based on the reportType

    //http://msdn.microsoft.com/en-us/library/ms155397.aspx

    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);

 

    //Clear the response stream and write the bytes to the outputstream

    //Set content-disposition to "attachment" so that user is prompted to take an action

    //on the file (open or save)

    Response.Clear();

    Response.ContentType = mimeType;

    Response.AddHeader("content-disposition", "attachment; filename=foo." + fileNameExtension);

    Response.BinaryWrite(renderedBytes);

    Response.End();

 

}

 

Note that if you change the ReportType in the Render method, you will also have to change the DeviceInfo settings.

Related Posts
Rendering an RDLC directly to the Response stream in ASP.NET MVC
Creating an ASP.NET report using Visual Studio 2010

FAQ


References:

35 Comments

  • Hi,
    Can you give an example of your GetData() method. &nbsp;When I view the rdlc source it contains all the datasource information, but I'm not sure how to go about generating these for use. I'm not using the report viewer control.
    Thanks in advance.

  • This example works great to send a simple local report directly to PDF. I now have a report on a web page on which I have 2 textboxes (a beginning date and an ending date) which the user will complete to identify a range, and I need to pass those 2 dates into the report. The report is using an SQL stored procedure that will take those parameters to send back a dataset. But I can't seem to get it to work. Can you give an example of how you would add parameters to this code? Thanks!

  • if reportType is "image" how to get the multiple pages to client side.

  • If its output is image, the extension is Tif, which seems to take care of pages by itself

  • Works great!
    Thanks Raj
    Florante

  • I can get 1 report to render as PDF and stream to IE great. What if I want to have 2 seperate reoprts run but appear as one PDF. I am currently trying to combine the bites before they are displayed and it is not working. Is this possible?

  • Hi,

    I use the method above, but the generated PDF files is very large. It seems that the DPI is very high and that yout can't change than when output is PDF. Tiff is just fine. Is there a work around for that?

    Best regards,
    Christian

  • I get the following message when the renderbytes... code is executed "LocalProcessingException was unhandled by user code"

    This only occurs on reports that are connected to a data source. &nbsp;A report with no data works fine. &nbsp;The report with data items works with out problems when viewed in the report viewer.

    Any assistance would be much appreciated.

  • I get the same problem with my PDF being 15Mb when directly rendered from report viewer. When opened in Adobe and saved, file shrinks to 192Kb. What the heck? I guess Microsofts PDF algorithm sucks big time compared to Adobe's. Have you found fix or way around large PDF File?

  • I would be very greatful too, getting a solution or workaround against the large PDF files generated by ReportViewer. We create reports including only 2 charts and we get PDF files of 17Mb !! Our customers asked us to change that.

  • What is the full namespace of the LocalReport object being created?

    Is it Microsoft.Reporting.WinForms.LocalReport or Microsoft.Reporting.WebForms.LocalReport? If so, is there a way to do this without using either of these? I need to do this because I have a c# class that does not have any UI at all but I still need to render the rdlc.

  • I need to export the report Into Word File Format, Is that possible, or is there a way around?

    Thanks,
    Dave

  • How to render the rdlc report to multiple sheets of Excel instead of single sheet

  • hi,
    can we generate report in PDF format, from the contents of a richtext box, the information to be filled in the data field shall be fetched from database.
    thanks,
    kalai

  • hi,

    i have generated pdf file using ReportViewer control. but the images are not getting displayed in the result pdf file. Can anyone please help me on this issue?

    Thanks,
    Sat

  • Thank you for posting this code, very helpful.

  • Hi,

    I have somewhat the same thing but I have outputformat EMF and I render Image.

    I want to show an image on a printer friendly page and use java print to print on client side. Here's my code:

    I changed the Response.ContentType = "image/tiff".


    Warning[] warnings;
    string[] streamids;
    string mimeType;
    string encoding;
    string extension;
    string deviceInfo =
    "" +
    " EMF" +
    " 10in" +
    " 11.5in" +
    " 0.0in" +
    " 0.0in" +
    " 0.0in" +
    " 0.0in" +
    "";

    byte[] bytes = ReportViewer.LocalReport.Render("image", deviceInfo, out mimeType, out encoding, out extension, out streamids, out warnings);
    MemoryStream s = new MemoryStream(bytes);


    How do I set the extension to tiff?
    Please help

  • Hi,
    Now, How to print the Response stream directly to printer ? Anyone can help me ?
    Thanks

  • Hi,

    Anyone can help me in How to print number of copies = 2.

    Thanks.

  • how we can output to xml format

  • How can we use this code to output reports to the following formats
    1. XML
    2. CSV
    3. TIFF

  • i need he pdf output to be readable in adobe 7, it does not seem to be, is anybody else having his same problem?

  • Thank you very much, I was looking for same. Its really helped me alot
    Thanks

  • I got error "LocalProcessingException" was unhandled on line,
    renderedBytes = rptLoc.Render(reportType, deviceInfo, out mimeType, out encoding, out fileNameExtension, out streams, out warnings);

    Can u give the solution please?

    Regards,
    Salim.

  • Good article
    You can also display the file size in the dialogue by adding the following after the header
    Response.AddHeader("Content-Length", _ renderedBytes.Length.ToString())


  • Great code sample. wish there were like these (extremely detailed and well-written).

  • i've got an error on part

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

    error is :
    Microsoft.Reporting.WinForms.LocalProcessingException: An error occurred during local report processing. ---> Microsoft.ReportingServices.ReportProcessing.ReportProcessingException: One or more parameters required to run the report have not been specified.

    anyone can solve that. am stuck here.. plese...
    thanx before

  • Thanks, Exactly what I needed!

  • How to I report developed as RDLC in CSV format? I read some where RDLC does not support exporting in CSV,MHTML which is supportedby RDLs. Is there any way to achive this.
    Regards,
    Darshan
    &nbsp;

  • Hi

    I have one question :
    I use just SSIS and SSRS, Pls tell me where do I write this code to render?
    Do I need to write in SSIS script task?
    Or somewhere in SSRS?

    Thanks

  • How is it Possible to Preview RDLC Report Like Sql Server Reporting Services Rdl Report

  • i want to render a report to HTML

    Thanks

  • I also implemented the same code but instead of asking for attachment open/save 'pdf' it asks to open/save 'Default.aspx' (current page)

    plz help
    waiting

  • Most excellent!!!

    I have used the same functionality to provide the ability to run the report back in an application tier and email the resulting PDF file to the specified user. We had issues with reports taking a long amount of time and we did not want to tie up a users machine.

    Great!

  • Just came across your post. Exactly what I've been looking for...thanks

  • Great code example! &nbsp;I have built a website that utilized the ReportViewer ASP.NET server control, but then ran into a wall when I discovered that the ReportViewer control would not function under partial trust. &nbsp;Since my web host runs my app under Medium Trust, I couldn't use the Server Control to render the reports. &nbsp;I thought your code example here could help out, but I still get the same error: &quot;That assembly does not allow partially trusted callers.&quot; &nbsp;Looks like I have to find another host (already paid for 2 years at current host!) or migrate to another technology.

Comments have been disabled for this content.