Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Exporting to .PDF file not working exactly correctly 1

Status
Not open for further replies.

ruthaustin

IS-IT--Management
Feb 4, 2003
5
0
0
US
I'm exporting a Crystal Report within a asp page in VB.NET to a .PDF file. I want to Redirect the file and display it so the "user" can print it. The problem is I get a blank page. When I hit refresh, it shows correctly... but I don't want to make people do that! Has anyone run into this problem and how did you solve it? VB code if possible, please! Thanks, Ruth
 
I am hopeful you have already tried this, but check the printer setup for the server. I have run into my share of weird problems because the default printer of the server (which Crystal 8.5 wants) not being the same as the one that I used in development. Crys 9 has an option to ignore the printer, but I have not used it. Why not load the report with Crystal's viewer and then the user can print from there?
 
I'm probably being ignorant, but I just placed a Crystal Report Viewer (dragged/dropped) on my asp page. The Crystal menu bar does not show any print button... So, I thought I would export the file to a "new page" so they could print it within Adobe. I'm using Crystal 9.1 within .NET. Here's the code:

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim myReport As CrystalReport3 = New CrystalReport3()
Dim param As New ParameterDiscreteValue()
Dim values As New ParameterValues()
Dim server As Web.HttpServerUtility
server = Me.Server

'Note : we are creating an instance of the strongly-typed Crystal Report file here.
param.Value = Trim(TextBox1.Text)
values.Add(param)
myReport.DataDefinition.ParameterFields(0).ApplyCurrentValues(values)

param = New ParameterDiscreteValue()
param.Value = Trim(TextBox2.Text)
values.Add(param)

'the second param in the report
myReport.DataDefinition.ParameterFields(1).ApplyCurrentValues(values)



Dim DiskOpts As CrystalDecisions.Shared.DiskFileDestinationOptions = New CrystalDecisions.Shared.DiskFileDestinationOptions()

myReport.ExportOptions.ExportDestinationType = CrystalDecisions.[Shared].ExportDestinationType.DiskFile

' You also have the option to export the report to other sources
' like Microsoft Exchange, MAPI, etc.

myReport.ExportOptions.ExportFormatType = CrystalDecisions.[Shared].ExportFormatType.PortableDocFormat

'Here we are exporting the report to a .pdf format. You can
' also choose any of the other formats specified above.
Dim strURL As String = "/reporttest"
Dim strFile As String = "Output.pdf"
Dim FilePath As String

Dim strPath = server.MapPath("Report")

DiskOpts.DiskFileName = strPath & "\" & strFile
'DiskOpts.DiskFileName = "c:\Output.pdf"
'If you do not specify the exact path here (i.e. including
' the drive and Directory),
'then you would find your output file landing up in the
'c:\WinNT\System32 directory - atleast in case of a
' Windows 2000 System

myReport.ExportOptions.DestinationOptions = DiskOpts

'The Reports Export Options does not have a filename property
'that can be directly set. Instead, you will have to use
'the DiskFileDestinationOptions object and set its DiskFileName
'property to the file name (including the path) of your choice.
'Then you would set the Report Export Options
'DestinationOptions property to point to the
'DiskFileDestinationOption object.


myReport.Export()


'This statement exports the report based on the previously set properties.
Me.Response.Redirect("Report\" & strFile)

End Sub

Thanks in advance, Ruth
 
I use both Crystal 9 & 8.5. Here is the code that I use --- there is one line that is different between reports (this one is a v9 report), but the samples that Crystal has on their site should help you too. Look at the parms that you can use on the call --- THAT will help a bit.

<%@ Language=VBScript %>
<%
basePath = Request.ServerVariables(&quot;PATH_TRANSLATED&quot;)
While (Right(basePath, 1) <> &quot;\&quot; And Len(basePath) <> 0)
iLen = Len(basePath) - 1
basePath = Left(basePath, iLen)
Wend

baseVirtualPath = Request.ServerVariables(&quot;PATH_INFO&quot;)
While (Right(baseVirtualPath, 1) <> &quot;/&quot; And Len(baseVirtualPath) <> 0)
iLen = Len(baseVirtualPath) - 1
baseVirtualPath = Left(baseVirtualPath, iLen)
Wend

If Not IsObject(session(&quot;oApp&quot;)) Then
Set session(&quot;oApp&quot;) = Server.CreateObject(&quot;CrystalRuntime.Application.9&quot;)
If Not IsObject(session(&quot;oApp&quot;)) Then
response.write &quot;Error: Could not instantiate the Crystal Reports automation server.&quot;
response.end
End If
End If


If IsObject(session(&quot;oRpt&quot;)) Then
set session(&quot;oRpt&quot;) = nothing
End If

Set session(&quot;oRpt&quot;) = session(&quot;oApp&quot;).OpenReport(basePath & &quot;PurchaseOrder.rpt&quot;, 1)
If Err.Number <> 0 Then
Response.Write &quot;Error Occurred creating Report Object: &quot; & Err.Description
Set Session(&quot;oRpt&quot;) = Nothing
Set Session(&quot;oApp&quot;) = Nothing
Session.Abandon
Response.End
End If

session(&quot;oRpt&quot;).DiscardSavedData

session(&quot;oRpt&quot;).MorePrintEngineErrorMessages = False
session(&quot;oRpt&quot;).EnableParameterPrompting = False


set crtable = session(&quot;oRpt&quot;).Database.Tables.Item(1)
crtable.SetLogonInfo cstr(&quot;*ODBC CONNECTION NAME*&quot;), cstr(&quot;*SQL DATABASE NAME*&quot;), cstr(&quot;*sql login HERE*&quot;), cstr(&quot;*sql pwd here*&quot;)
if not crtable.TestConnectivity then
response.write &quot;ReCrystallize Warning: Unable to connect to data source using the following information.<BR><BR>&quot;
response.write &quot;Server / ODBC data source: *ODBC CONNECTION NAME*<BR>&quot;
response.write &quot;Database / Table: *SQL DATABASE NAME*<BR>&quot;
response.write &quot;User Name: &quot; & &quot;*sql login HERE*<BR>&quot;
response.write &quot;Please verify the database user password in this ASP file.<BR><BR>&quot;
end if


set session(&quot;ParamCollection&quot;) = Session(&quot;oRpt&quot;).Parameterfields

set Param1 = session(&quot;ParamCollection&quot;).Item(1)
ParamValue1 = Request(&quot;PO_Number&quot;)
Call Param1.SetCurrentValue (CDbl(ParamValue1), 7)

If IsObject (session(&quot;oPageEngine&quot;)) Then
set session(&quot;oPageEngine&quot;) = nothing
End If

set session(&quot;oPageEngine&quot;) = session(&quot;oRpt&quot;).PageEngine

%>
<HTML>
<HEAD>
<TITLE>
TITLE FOR PAGE
</TITLE>
</HEAD>
<BODY BGCOLOR=C6C6C6 LANGUAGE=VBScript ONLOAD=&quot;Page_Initialize&quot; ONUNLOAD=&quot;CallDestroy()&quot; leftmargin=0 topmargin=0 rightmargin=0 bottommargin=0>
<OBJECT ID=&quot;CRViewer&quot;
CLASSID=&quot;CLSID:2DEF4530-8CE6-41c9-84B6-A54536C90213&quot;
WIDTH=100% HEIGHT=100%
CODEBASE=&quot;/viewer9/activeXViewer/activexviewer.cab#Version=9,2,0,442&quot; VIEWASTEXT>
<PARAM NAME=&quot;EnableRefreshButton&quot; VALUE=0>
<PARAM NAME=&quot;EnableGroupTree&quot; VALUE=0>
<PARAM NAME=&quot;DisplayGroupTree&quot; VALUE=0>
<PARAM NAME=&quot;EnableExportButton&quot; VALUE=1>
<PARAM NAME=&quot;EnablePrintButton&quot; VALUE=1>
<PARAM NAME=&quot;EnableDrillDown&quot; VALUE=1>
<PARAM NAME=&quot;EnableAnimationControl&quot; VALUE=1>
<PARAM NAME=&quot;EnableZoomControl&quot; VALUE=1>
<PARAM NAME=&quot;EnableSearchControl&quot; VALUE=1>
<PARAM NAME=&quot;DisplayToolbar&quot; VALUE=1>
<PARAM NAME=&quot;EnableProgressControl&quot; VALUE=1>
<PARAM NAME=&quot;EnableStopButton&quot; VALUE=1>
<PARAM NAME=&quot;EnableCloseButton&quot; VALUE=1>
<PARAM NAME=&quot;EnableNavigationControls&quot; VALUE=1>
<PARAM NAME=&quot;PromptOnRefresh&quot; VALUE=0>
<PARAM NAME=&quot;EnablePopupMenu&quot; VALUE=1>
<PARAM NAME=&quot;DisplayBackgroundEdge&quot; VALUE=1>
<PARAM NAME=&quot;DisplayBorder&quot; VALUE=1>
<PARAM NAME=&quot;DisplayTabs&quot; VALUE=1>
</OBJECT>

<SCRIPT LANGUAGE=&quot;VBScript&quot;>
<!--
zoomTimer = window.setInterval(&quot;setZoom&quot;,250)

Sub setZoom()
if not CRViewer.IsBusy then
window.clearInterval(zoomTimer)
CRViewer.Zoom(100)
end if
End Sub
Sub Page_Initialize
On Error Resume Next
Dim webBroker
Set webBroker = CreateObject(&quot;WebReportBroker9.WebReportBroker&quot;)
if ScriptEngineMajorVersion < 2 then
window.alert &quot;Internet Explorer 3.02 users running Windows NT 4 should get the latest version of VBScript or install IE 4.01 SP1. IE 3.02 users running Windows 95 need DCOM95 and the latest version of VBScript, or install IE 4.01 SP1. These files are available at Microsoft's web site.&quot;
CRViewer.ReportName = Location.Protocol + &quot;//&quot; + Location.Host + <% response.write( chr(34) & baseVirtualPath & chr(34)) %> + &quot;rptserver.asp&quot;
else
Dim webSource
Set webSource = CreateObject(&quot;WebReportSource9.WebReportSource&quot;)
webSource.ReportSource = webBroker
webSource.URL = Location.Protocol + &quot;//&quot; + Location.Host + <% response.write( chr(34) & baseVirtualPath & chr(34)) %> + &quot;rptserver.asp&quot;
webSource.PromptOnRefresh = False
CRViewer.ReportSource = webSource
end if
CRViewer.ViewReport
End Sub
-->
</SCRIPT>
<script language=&quot;javascript&quot;>
function CallDestroy()
{
window.open(&quot;Cleanup.asp&quot;,&quot;Cleanup&quot;,&quot;top=2000&quot;);
}
</script>

</BODY>
</HTML>
 
Thank you for trying to help. I actually found code that works for me on an asp.net forum and will include it below. I guess that Crystal Report toolbars inside and outside of .NET are different?... Cheers, Ruth

Here's the paste from the asp.net forum:

I'm of the view that if you want the client to print reports, it's best to export them to pdf format. Adobe then takes care of rendering the report properly for your client's printer. It's actually easy to do this, or I wouldn't suggest it:

This exports the report to a file on the server:

Dim objCrystalFileDestinationOptions As New DiskFileDestinationOptions()

'Export the report in pdf format.
objCrystalFileDestinationOptions.DiskFileName = strAdobeFileName
_objCrystalReportDocument.ExportOptions.DestinationOptions = objCrystalFileDestinationOptions
_objCrystalReportDocument.ExportOptions.ExportDestinationType = ExportDestinationType.DiskFile
_objCrystalReportDocument.ExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat
_objCrystalReportDocument.Export()

and this sends that file to the browser:

HttpContext.Current.Response.ClearHeaders()
HttpContext.Current.Response.ClearContent()
HttpContext.Current.Response.ContentType = &quot;application/pdf&quot;
HttpContext.Current.Response.WriteFile(strAdobeFileName)
HttpContext.Current.Response.Flush()
HttpContext.Current.Response.Close()
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top