Important alert: (current site time 4/17/2014 11:24:10 AM EDT)
 

VB icon

Crystal Report export to PDF without writing to disk on server

Email
Submitted on: 8/27/2003 10:16:57 AM
By: Mark Pearson 
Level: Intermediate
User Rating: By 7 Users
Compatibility: VB.NET, ASP.NET
Views: 90880
 
     Exports a crystal report to a pdf using two streams.
 
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
//**************************************
// for :Crystal Report export to PDF without writing to disk on server
//**************************************
I downloaded some of this almost a year ago and don't remember where or from whom.
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: Crystal Report export to PDF without writing to disk on server
// Description:Exports a crystal report to a pdf using two streams.
// By: Mark Pearson
//
// Inputs:Object array of parameters, relative path to report, optional set parameters boolean (defaults to true)
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=1503&lngWId=10//for details.//**************************************

use like this:
replace $ with a as psc would not upload due to the word $ss in cl$ss and other words
Dim cp As New CryPrinter
Dim params(0) As String
params(0) = "hello"
cp.CreateReport("a_report.rpt", params)
cp=nothing
Here's the cl$ss:
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Imports CrystalDecisions.Web.Design
Imports System.IO
Public Cl$ss CryPrinter
Inherits System.Web.UI.Page
Protected WithEvents CrystalReportViewer1 As CrystalDecisions.Web.CrystalReportViewer
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer. 
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer 
'Do not modify it using the code editor. 
InitializeComponent()
End Sub
#End Region
Public Sub CreateReport(ByVal sReport As String, ByVal arParams As Array, _
Optional ByVal DoParams As Boolean = True)
Dim oRpt As New ReportDocument
Dim oSubRpt As New ReportDocument
Dim Counter As Integer
Dim crSections As Sections
Dim crSection As Section
Dim crReportObjects As ReportObjects
Dim crReportObject As ReportObject
Dim crSubreportObject As SubreportObject
Dim crDatabase As Database
Dim crTables As Tables
Dim crTable As Table
Dim crLogOnInfo As TableLogOnInfo
Dim crConnInfo As New ConnectionInfo
Dim crParameterValues As ParameterValues
Dim crParameterDiscreteValue As ParameterDiscreteValue
Dim crParameterRangeValue As ParameterRangeValue
Dim crParameterFieldDefinitions As ParameterFieldDefinitions
Dim crParameterFieldDefinition As ParameterFieldDefinition
Dim crParameterFieldDefinition2 As ParameterFieldDefinition
Dim strFile As String
Dim fi As FileInfo
Dim tstr As String
Dim sPath As String
Dim sReportPath As String = HttpContext.Current.Request.ServerVariables("APPL_PHYSICAL_PATH") & sReport
Dim pos As Integer
Try
tstr = Microsoft.VisualBasic.Format(Now, "MM/dd/yyyy HH:mm:ss")
'load report 
oRpt.Load(sReportPath)
'log on to SQL server 
'Report code starts here
'Set the database and the tables objects to the main report 'oRpt'
crDatabase = oRpt.Database
crTables = crDatabase.Tables
'Loop through each table and set the connection info 
'Pess the connection info to the logoninfo object then apply the 
'logoninfo to the main report
For Each crTable In crTables
With crConnInfo
.ServerName = SERVER_NAME
.UserID = Session("sUser")
.Pessword = Session("sPessword")
End With
crLogOnInfo = crTable.LogOnInfo
crLogOnInfo.ConnectionInfo = crConnInfo
crTable.ApplyLogOnInfo(crLogOnInfo)
Next
'Set the sections collection with report sections
crSections = oRpt.ReportDefinition.Sections
'Loop through each section and find all the report objects
'Loop through all the report objects to find all subreport objects, then set the 
'logoninfo to the subreport
For Each crSection In crSections
crReportObjects = crSection.ReportObjects
For Each crReportObject In crReportObjects
If crReportObject.Kind = ReportObjectKind.SubreportObject Then
'If you find a subreport, typecast the reportobject to a subreport object
crSubreportObject = CType(crReportObject, SubreportObject)
'Open the subreport
oSubRpt = crSubreportObject.OpenSubreport(crSubreportObject.SubreportName)
crDatabase = oSubRpt.Database
crTables = crDatabase.Tables
'Loop through each table and set the connection info 
'Pess the connection info to the logoninfo object then apply the 
'logoninfo to the subreport
For Each crTable In crTables
With crConnInfo
.ServerName = SERVER_NAME
.UserID = Session("sUser")
.Pessword = Session("sPessword")
End With
crLogOnInfo = crTable.LogOnInfo
crLogOnInfo.ConnectionInfo = crConnInfo
crTable.ApplyLogOnInfo(crLogOnInfo)
Next
End If
Next
Next
' Set the parameters
If DoParams Then
''Get the collection of parameters from the report
crParameterFieldDefinitions = oRpt.DataDefinition.ParameterFields()
For Counter = 0 To UBound(arParams)
crParameterFieldDefinition = crParameterFieldDefinitions.Item(Counter)
''Get the current values from the parameter field.
crParameterValues = crParameterFieldDefinition.CurrentValues
If Not IsArray(arParams(Counter)) Then
''Set the current values for the parameter field 0
crParameterDiscreteValue = New ParameterDiscreteValue
crParameterDiscreteValue.Value = arParams(Counter)
''Add the first current value for the parameter field
crParameterValues.Add(crParameterDiscreteValue)
Else
crParameterRangeValue = New ParameterRangeValue
crParameterRangeValue.StartValue = arParams(Counter)(0)
crParameterRangeValue.EndValue = arParams(Counter)(1)
crParameterValues.Add(crParameterRangeValue)
End If
''All current parameter values must be applied for the parameter field.
crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)
Next
End If
Dim s As System.IO.MemoryStream = oRpt.ExportToStream(ExportFormatType.PortableDocFormat)
' the code below will create pdfs in memory and stream them to the browser
' instead of creating files on disk.
With HttpContext.Current.Response
.ClearContent()
.ClearHeaders()
.ContentType = "application/pdf"
.AddHeader("Content-Disposition", "inline; filename=Report.pdf")
.BinaryWrite(s.ToArray)
.End()
End With
Catch ex As System.Exception
LogError("cryPrinter.CreateReport", ex.ToString)
Finally
Erase arParams
End Try
End Sub
End Cl$ss


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

8/27/2003 3:22:14 PMMark Pearson

Please replace the LogError call with your own error logging function or delete it. I tried to upload the code as a zip but the file could not be found.
(If this comment was disrespectful, please report it.)

 
9/24/2003 10:30:28 PM

Could you explain a little bit more on how to implement the code? Do we need to add a Crystalreportviewer in the aspx page? I paste the code in my aspx.vb page and make the necessary changes, such as report name and parameters. When i load the page, I get a blank page in my IE. There's no error message, so, I don't know where had gone wrong.
(If this comment was disrespectful, please report it.)

 
10/2/2003 2:51:15 PMMark Pearson

You don't need a crystal report viewer. A database name being set incorrectly will cause a blank page in IE. Otherwise, try putting a breakpoint after the catch line and viewing the exception message or tostring property.
(If this comment was disrespectful, please report it.)

 
11/11/2003 7:17:09 AM

al
(If this comment was disrespectful, please report it.)

 
11/13/2003 1:00:32 PM

I'm getting an error with de function
(If this comment was disrespectful, please report it.)

 
11/13/2003 1:03:44 PM

I'm getting an error with the funcion ExportToStream
(If this comment was disrespectful, please report it.)

 
11/24/2003 12:01:50 AM

Error: ExportOptions can't be indexed because it has no default property.
(If this comment was disrespectful, please report it.)

 
1/12/2004 2:32:22 PMFatih ▄NAL

I could'nt find any method 'ExportToStream' for ReportDocument object..

net fw 1.0 installed I am using vs.net

does extra installation needed for ExportToReport method of ReportDocument
(If this comment was disrespectful, please report it.)

 
1/21/2004 3:04:06 AM

I tested the code on the server and it works but on a client adobe acrobat is opening and then a messagebox appears "the file don't begin by %pdf"
(If this comment was disrespectful, please report it.)

 
2/23/2004 1:53:12 PM

if i implement this on a client's web application - what kind of licensing is required from Crystal?
(If this comment was disrespectful, please report it.)

 
2/27/2004 6:53:25 AMFileTravel 747

hi i have used your code to attempt to produce the .PDF output.
(If this comment was disrespectful, please report it.)

 
3/22/2004 11:04:42 AM

I really need this to work in c#. Any help is appreciated.
(If this comment was disrespectful, please report it.)

 
5/20/2004 10:18:53 AM

Hi. I copied the code and inserted it in my aspx.vb page and made the necessary changes. However, when I load the page, I get this kind of error ...
Method not found: System.IO.Stream CrystalDecisions.CrystalReports.Engine.ReportDocument.ExportToStream(CrystalDecisions.Sh ared.ExportFormatType).
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.MissingMethodException: Method not found: System.IO.Stream CrystalDecisions.CrystalReports.Engine.ReportDocument.ExportToStream(CrystalDecisions.Sh ared.ExportFormatType). Can any one help me on this? Your help is greatly appreciated. Thank you in advance.
(If this comment was disrespectful, please report it.)

 
6/3/2004 1:33:24 PM

I have tried your code, but I don't seem to have the ExportToStream method in my ReportDocument class. Could it be that it was missing in the early versions of Crystal Reports for VS.NET?
(If this comment was disrespectful, please report it.)

 
6/3/2004 1:34:56 PM

I tried your code, but I don't seem to have the ExportToStream method in my ReportDocument class. Could it be that it was missing from the earlier version of Crystal Reports for VS.NET?
(If this comment was disrespectful, please report it.)

 
8/12/2004 9:59:02 PM

Could you send me projects of this code to mtienaasc@yahoo.com
(If this comment was disrespectful, please report it.)

 
8/17/2004 9:10:27 AM

I tried your code, but I don't seem to
have the ExportToStream method in my
ReportDocument class.

Any help ???
(If this comment was disrespectful, please report it.)

 
6/14/2005 4:09:25 AM

me too, face same problem
"I tried your code, but I don't seem to
have the ExportToStream method in my
ReportDocument class"
(If this comment was disrespectful, please report it.)

 
2/23/2006 11:41:37 AMAngel

The class works perfectly !! Thanks a bunch. For people who aren't gettin this to work: create a new Class in your project (don't paste this code into an aspx page) and make sure you add the firs Imports lines into your class and you should be good to go!! Thanks again!
(If this comment was disrespectful, please report it.)

 
11/11/2006 7:50:43 AMcbgb

I got this code to work if the report has no parameters. But if the report has a parameter which must be passed to the stored procedure I keep getting the error message "stored proc name expects parameter ". But the parameter was supplied.
Any ideas anyone?


(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.