Link to home
Start Free TrialLog in
Avatar of FYPJ
FYPJ

asked on

Program to save CR9 generated PDF into system

Hi,

I have a sample code that automatically exports a Crystal Report into PDF and displays it in a browser.

What I need to do now is to save this .pdf file into my harddisk without having the user to manually click File->Save As...

Is there an object or function to execute such a task?

I'm using ASP.

Here is the sample code I'm using.



<%@ Language=VBScript CodePage=65001 %>

<%
'=====================================================================
' WORKING WITH RAS AND ASP TO EXPORT REPORTS
'=====================================================================

' This line creates a string variable called reportName that we will use to pass
' the Crystal Report file name (.rpt file) to the OpenReport method

reportName = "SimpleReportExport.rpt"

' Use the Object Factory object to create other RAS objects (useful for versioning changes)
Set objFactory = CreateObject("CrystalReports.ObjectFactory")
      
' This "While/Wend" loop is used to determine the physical path (eg: C:\) to the
' Crystal Report .rpt by translating the URL virtual path (eg: http://Domain/Dir)  
Dim path, iLen
path = Request.ServerVariables("PATH_TRANSLATED")                    
While (Right(path, 1) <> "\" And Len(path) <> 0)                      
iLen = Len(path) - 1                                                  
path = Left(path, iLen)                                              
Wend      
                                                                                                                                   
' Create a new ReportClientDocument object
Set oClientDoc = objFactory.CreateObject("CrystalClientDoc.ReportClientDocument")

' Specify the RAS Server (computer name or IP address) to use.
' This property should be used ONLY when the SDK and RAS Service are running on separate machines.
'Session("oClientDoc").ReportAppServer = "[COMPUTER NAME]"

' Open the report object to initialize the ReportClientDocument
oClientDoc.Open path & reportName


'==================================================================
' WORKING WITH PRINT OUTPUT CONTROLLER
'==================================================================

Dim byteArray, bufArray

' Constants - All available export format types
crReportExportFormatCrystalReports = 0                        'Crystal Report
crReportExportFormatMSWord  = 1                               'Microsoft Word
crReportExportFormatMSExcel = 2                                    'Microsoft Excel
crReportExportFormatRTF = 3                                          'Rich Text Format
crReportExportFormatPDF = 5                                          'Adobe PDF
crReportExportFormatRecordToMSExcel = 6                        'Microsoft Excel (Data Only)
crExportFormatText = 7                                                'Plain Text
crReportExportFormatCharacterSeparatedValues = 8      'Comma Separated Values


' Retrieve byteArray from the PrintOutputController
Set byteArray = oClientDoc.PrintOutputController.Export(crReportExportFormatPDF)


'==================================================================
' SETUP THE RESPONSE OBJECT ATTRIBUTES
'==================================================================

' Clear the Response object
Response.Clear

' Add a custom header for an inline file type with a default file name
Response.AddHeader "content-disposition", "inline;filename=CrystalReport.pdf"

' Set the Content Type to application/pdf
Response.ContentType = "application/pdf"

' Send the bytearray to the browser through the Response object, used
' detachArray method for quicker tranfer of data
Response.BinaryWrite byteArray.detachArray

%>
Avatar of Shay79
Shay79

hi
This is what I use for Version 10 - should be similar to 9
  crDiskFileDestinationOptions = New DiskFileDestinationOptions
  crExportOptions = crReportDocument.ExportOptions
 crDiskFileDestinationOptions.DiskFileName = FileName 'path of where to save file

            'set the required report ExportOptions properties
            With crExportOptions
                .DestinationOptions = crDiskFileDestinationOptions
                .ExportDestinationType = ExportDestinationType.DiskFile
                .ExportFormatType = ExportFormatType.PortableDocFormat
            End With
crReportDocument.Export()
Avatar of Mike McCracken
That should work with CR9.  However you will only get the file saved and not displayed.

If you want it displayed and saved you either have to let the user save it, save it and use calls to open the PDF file, or basically rubn the report twice (once to save then again to display it).

mlmcc
Avatar of FYPJ

ASKER

Thanks guys! I have tried placing the codes at the end of my program but there is an error msg that says " DiskFileDestinationOptions
 no defined".

Where should I or how should I place the codes and does it clash with my current program?/

Thanks!
When you enter this part

            With crExportOptions
                .DestinationOptions =

you should get a popup of valid values.  Pick the one that is for disk file

mlmcc
Avatar of FYPJ

ASKER

Sorry I'm a rookie so is it possible to explain in details wat u mean in the previous post?

Thanks alot!
Hi
The destination options define where it will be exported to. If you look for this in you object manager you should see a list of valid values (eg: File).

Then after its been exported to pdf, you can display it by passing the filename to a new page, and in the code display it

Response.ContentType = "Application/pdf"
Response.WriteFile(Request.QueryString("FileName"))
Response.End()
Avatar of FYPJ

ASKER

Hi,

I'm still getting the "Class not defined: 'DiskFileDestinationOptions'" error.

Must I import or add something?

Is there a sample code somewhere for such a situation? I'm kinda lost here.
Hi

The code I posted was just a pointer, as I mentioned its for version 10. But I actually just did a quick search on the Business Objects website and found this snippet- please note I havent tried it as I am running version 10


Symptom
When you export a report to disk using either the Crystal Enterprise (CE) or Report Application Server (RAS) SDK, how can you save the resulting ByteArray object to disk?

Resolution
To save the ByteArray object to disk, use the 'Save' method as follows:


• To save the ByteArray to the web application folder, use the following sample code:


byteArray.save(Server.MapPath(".") & "\" & "export.pdf")


• To save the byteArray to a different folder, use the following sample code:


byteArray.save("C:\export.pdf")


====================

NOTE:


When saving the ByteArray to a folder other than the application folder, explicitly give the user account under which IIS is running write access to this folder by completing these steps:


1. Right-click the folder and click 'Properties'.


2. Click the 'Security' tab.


3. Click the 'Add' button. The 'Select Users, Computers, or Groups' dialog box appears.


4. Type the name of the user account under which IIS is running.


5. Click 'OK'.


6. Select the 'Write' box.


7. Click 'OK'.

ASKER CERTIFIED SOLUTION
Avatar of Shay79
Shay79

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of FYPJ

ASKER

Hey thanks Shay79 thats what i need.
glad to help!