Programattically render pdf from ReportViewer

I have created several embedded ReportViewer reports in asp.net
2.0 (using VB). Love the ReportViewer. Used Cognos SDK up to 3 wks ago,
but not any more!


When my app's user pushes a button, 8 diferent reports come up in
separate browser windows (e.g.
Response.Write("<script>window.open('../Reports/rptOversale_Compare.aspx
','_OverSaleCompare');</script>")).


What I want to do is have the reports come up in pdf format. Is this
possible The ideal objective for this app would be to have the 8
reports come up as pdf inside a tab control, but I can't find any code
or direction anywhere on how to programmatically spit out pdf instead of
ReportViewer default display. I'll worry about the tab part after getting pdf to come up.


Does anyone have any code or insight that would accomplish this
Thank you for any help!!


Answer this question

Programattically render pdf from ReportViewer

  • Tuoski

    Is it possible to attach the rendered Page as PDF in an email and send directly. My main goal is to send across the PDF report as an email upon a button click

  • SmitsDJ

    I'm using this:

    Response.Write("<script>window.open('../ReportOutput/PCSummary.PDF','_PCSummary');</script>")

    Anyone know the difference is between this and the Response.WriteFile approach. Maybe the new browser window. Paully - any thoughts


  • TOP1411

    Adnan -

    Wow, thank you very much for this code!

    I'm sorry, I forgot to mention I'm not using Reporting Svcs; ReportViewer is in local mode and I doubt the group that supports the database server has any plans to upgrade to SQL Server 2005 w/Reporting Svcs.

    Is this a deal killer then for being able to do this with all Microsoft technology

    Any feel for 3rd party products I might be able to use

    Thanks again!


  • James_AeroAvi

    Simi - Do you want the PDF to be an attachment or embedded in the body of the email
  • snickers_kin

    You can render to PDF by calling ReportViewer.LocalReport.Render() and specifying "PDF" for the format. Then this byte stream can be used any way you want in your application.
  • Bryn Kaufman

    Yes, this works perfectly. Thank you very much!! For others, here are the details:

    Dim warnings As Warning() = Nothing

    Dim streamids As String() = Nothing

    Dim mimeType As String = Nothing

    Dim encoding As String = Nothing

    Dim extension As String = Nothing

    Dim bytes As Byte()

    'Get folder on web server from web.config

    Dim FolderLocation As String

    FolderLocation = System.Configuration.ConfigurationManager.AppSettings("ReportOutputPath")

    'First delete existing file

    Dim filepath As String = FolderLocation & "PCSummary.PDF"

    File.Delete(filepath)

    'Then create new pdf file

    bytes = ReportViewer1.LocalReport.Render("PDF", Nothing, mimeType, _

    encoding, extension, streamids, warnings)

    Dim fs As New FileStream(FolderLocation & "PCSummary.PDF", FileMode.Create)

    fs.Write(bytes, 0, bytes.Length)

    fs.Close()


  • hypersw

    Using a Windows Forms application...

    1. Import the System.Diagnostics namespace.

    2. After your fs.Close() type the following...

       Process.Start(FolderLocation & "PCSummary.PDF")

    Using ASP.Net  (Courtesy of http://support.microsoft.com/default.aspx scid=kb;en-us;q307603)...

      'Set the appropriate ContentType.
       Response.ContentType = "Application/pdf"

      'Get the physical path to the file.
       Dim FilePath As String = FolderLocation & "PCSummary.PDF"

      'Write the file directly to the HTTP output stream.
       Response.WriteFile(FilePath)
       Response.End()



  • Pivinski

    once again thanks - that looks like it

  • Yi Liang

    Thank you very much for this code - How would i wire this up to a button command so that when clicked - the pdf report is created as above, then rendered by browser. In .Net-1.1 i used a ReportServer and my button simply called the render.PDF command - and the user was prompted to download or open the PDF

    Thanks Again


  • Derek Comingore - RSC

    The Response.Write approach renders the output to pdf in the same browser window. Also works great. Here's a full copy of my code:

    Imports System.IO

    Imports Microsoft.Reporting.WebForms

    Partial Class Reports_rpt_PCSumByMerchant

    Inherits System.Web.UI.Page

    Protected Sub Page_SaveStateComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SaveStateComplete

    Dim warnings As Warning() = Nothing

    Dim streamids As String() = Nothing

    Dim mimeType As String = Nothing

    Dim encoding As String = Nothing

    Dim extension As String = Nothing

    Dim bytes As Byte()

    'Get folder on web server from web.config

    Dim FolderLocation As String

    FolderLocation = System.Configuration.ConfigurationManager.AppSettings("ReportOutputPath")

    'First delete existing file

    Dim filepath As String = FolderLocation & "PCSumByMerchant.PDF"

    File.Delete(filepath)

    'Then create new pdf file

    bytes = ReportViewer1.LocalReport.Render("PDF", Nothing, mimeType, encoding, extension, streamids, warnings)

    Dim fs As New FileStream(FolderLocation & "PCSumByMerchant.PDF", FileMode.Create)

    fs.Write(bytes, 0, bytes.Length)

    fs.Close()

    '####Paully's code

    'Set the appropriate ContentType.

    Response.ContentType = "Application/pdf"

    'Write the file directly to the HTTP output stream.

    Response.WriteFile(filepath)

    Response.End()

    End Sub

    End Class


  • John Mandia

    will try that - thanks

    Paully - This is a web application - thanks again


  • Gr&amp;#233;goire de Jabrun

    In ASP.NET YOu can just send to the browser

    myBytes = ReportViewer.LocalReport.Render("PDF", Nothing, mimeType, encoding, extension, streamids, warnings)

    Response.Buffer = True
    Response.Clear()
    Response.ContentType = mimeType
    Response.AddHeader("content-disposition", "attachment; filename=myfile." + extension)
    Response.BinaryWrite(myBytes)
    Response.Flush()
    Response.End()


  • vcspec

    I would like to keep it as an attachment in the email

  • JWill

    Yes, assuming you are using Reporting Services to serve up the report to start with. The process involves calling the report server and streaming the resulting report via HttpWebResponse. Setting the response content type to pdf will stream the pdf to the users browser window, and open the document as a pdf document.

    The following is curtousy of Adnan Masood's blog at http://www.axisebusiness.com/adnano/PermaLink,guid,4f0b053f-2c4b-499a-8913-72b7220a6f71.aspx

    Dim ReportUrl As String = "http:///ReportServer %2f%&rs%3aCommand=Render&rs%3AFormat=PDF"

    Dim ReportWebRequest As HttpWebRequest = CType(WebRequest.Create(ReportUrl), HttpWebRequest)
    ReportWebRequest.Timeout = 10000
    ReportWebRequest.Credentials = CredentialCache.DefaultCredentials

    Dim ReportWebResponse As HttpWebResponse = CType(ReportWebRequest.GetResponse(), HttpWebResponse)
    Dim ReportResponseStream As StreamReader = New StreamReader(ReportWebResponse.GetResponseStream(), New
    UnicodeEncoding)
    Dim objMemoryStream As New MemoryStream(New UnicodeEncoding().GetBytes(ReportResponseStream.ReadToEnd()))

    Response.Clear()
    Response.AddHeader("Accept-Header", objMemoryStream.Length.ToString())
    Response.ContentType = "application/pdf"
    Response.OutputStream.Write(objMemoryStream.ToArray(), 0, Convert.ToInt32(objMemoryStream.Length))
    ReportResponseStream.Close()
    Response.Flush()

    Try
    Response.End()
    Catch
    End Try

    Hope this helps



  • Programattically render pdf from ReportViewer