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

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://msdn2.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:

Published Thursday, March 2, 2006 9:12 PM by rajbk
Filed under: , , , ,

Comments

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

Hi,

Can you give an example of your GetData() method.  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.

Tuesday, October 3, 2006 3:36 AM by Boris

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

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!

Monday, November 6, 2006 3:53 PM by Linda M.

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

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

Thursday, December 28, 2006 5:24 AM by chatura

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

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

Friday, January 19, 2007 3:39 PM by Kev Hunter

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

Works great!

Thanks Raj

Florante

Sunday, March 25, 2007 4:55 AM by Florante Navaja

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

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?

Thursday, April 12, 2007 4:49 PM by Clint

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

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

Tuesday, April 17, 2007 8:26 AM by Christian

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

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.  A report with no data works fine.  The report with data items works with out problems when viewed in the report viewer.

Any assistance would be much appreciated.

Sunday, June 17, 2007 12:52 AM by Roger Smith

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

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?

Tuesday, July 3, 2007 4:06 PM by Jason

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

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.

Monday, July 9, 2007 7:14 AM by Wieland Weike

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

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.

Monday, November 26, 2007 5:22 PM by Mike Claymon

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

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

Thanks,

Dave

Wednesday, November 28, 2007 2:28 PM by Dave Gold

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

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

Tuesday, January 8, 2008 11:40 PM by Syam

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

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

Wednesday, January 16, 2008 12:18 AM by kalai

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

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

Tuesday, February 26, 2008 10:32 AM by Sat

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

Thank you for posting this code, very helpful.

Tuesday, March 11, 2008 9:44 AM by Brian

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

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 =

                "<DeviceInfo>" +

                "  <OutputFormat>EMF</OutputFormat>" +

                "  <PageWidth>10in</PageWidth>" +

                "  <PageHeight>11.5in</PageHeight>" +

                "  <MarginTop>0.0in</MarginTop>" +

                "  <MarginLeft>0.0in</MarginLeft>" +

                "  <MarginRight>0.0in</MarginRight>" +

                "  <MarginBottom>0.0in</MarginBottom>" +

                "</DeviceInfo>";

               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

Tuesday, March 25, 2008 4:55 AM by Corna

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

Hi,

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

Thanks

Saturday, May 3, 2008 10:49 PM by LIL

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

Hi,

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

Thanks.

Friday, May 23, 2008 3:22 AM by Zynie

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

how we can output to xml format

Thursday, June 19, 2008 9:46 AM by suyog kale

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

How can we use this code to output reports to the following formats

1. XML

2. CSV

3. TIFF

Friday, July 4, 2008 4:57 PM by Otto

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

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

Thursday, August 14, 2008 12:08 PM by tarun ahuja

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

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

Thanks

Monday, August 18, 2008 11:59 AM by VM

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

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.

Thursday, December 18, 2008 6:21 AM by salimudheen

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

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

Friday, December 19, 2008 6:25 AM by Pal Marriott

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

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

Wednesday, February 11, 2009 11:32 AM by CC

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

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

Monday, March 9, 2009 11:58 PM by ian

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

Thanks, Exactly what I needed!

Thursday, April 2, 2009 4:59 AM by hein

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

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

 

Sunday, May 24, 2009 2:46 AM by darshan

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

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

Thursday, October 22, 2009 10:26 AM by ria

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

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

Monday, November 16, 2009 2:16 AM by Siamak

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

i want to render a report to  HTML

Thanks

Tuesday, November 17, 2009 3:39 AM by Saikrishna

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

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

Monday, November 23, 2009 7:18 AM by Riddhi

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

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!

Tuesday, February 23, 2010 12:11 PM by Scott

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

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

Sunday, March 21, 2010 11:15 PM by Peter

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

Great code example!  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.  Since my web host runs my app under Medium Trust, I couldn't use the Server Control to render the reports.  I thought your code example here could help out, but I still get the same error: "That assembly does not allow partially trusted callers."  Looks like I have to find another host (already paid for 2 years at current host!) or migrate to another technology.

Saturday, August 7, 2010 11:17 AM by Dave Hamdan