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

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.

FAQ


References:

Published Thursday, March 02, 2006 9:12 PM by rajbk

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 03, 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 06, 2006 3:53 PM by Linda M.

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

I know that there is an Excel export but is there also a way to export it as CSV?

Monday, November 27, 2006 3:53 AM by Alex

# 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 03, 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 09, 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

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

It worked perfectly

Thankyou

Here is above code in vb.net

private sub RenderReport(){

Dim localReport As New Microsoft.Reporting.WebForms.LocalReport

localReport.DataSources.Add(rds)

 Dim reportPath As String = Server.MapPath("Rep.RDLC")

localReport.ReportPath = reportPath

       Dim reportType As String = "PDF"

       Dim mimeType As String

       Dim encoding As String

       Dim fileNameExtension As String

       'The DeviceInfo settings should be changed based on the reportType

       'msdn2.microsoft.com/.../ms155397.aspx

       Dim deviceInfo As String = "<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>"

       Dim warnings As Microsoft.Reporting.WebForms.Warning()

       Dim streams As String()

       Dim renderedBytes As Byte()

       'Render the report

       renderedBytes = localReport.Render(reportType, deviceInfo, mimeType, encoding, fileNameExtension, streams, _

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

}

Thursday, February 28, 2008 6:36 AM by Noman

# 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 03, 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 04, 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

Leave a Comment

(required) 
(required) 
(optional)
(required)