In this article, Matt shows us how to use Adobe Active Reports in our ASP web pages using a custom made VB component.This article is a quick example on inserting Adobe report documents into a browser and using Adobe Reader to display them.
I am using MS 2000 Server, IIS5.0 and ASP to implement this article's functions. Additionally, you will need Adobe Reader installed on your machine, VB6.0 and Active Reports. (Adobe Reader is a free download.)
In the support file is a picture of the project in VB6.0.
The process of presenting the document is 1) to build and compile a VB COM object using Active Reports to output the report, and 2) calling the COM object from the server code in an ASP page to present the report.
An example of this can be found at http://www.datadynamics.com/default.aspx ...however, I accomplished this task a little differently, and I think it’s useful for other programmers to see it.
Our VB object uses a single function to call the designers needed to create the report:
' VB6 code of Generalized function to generate the report. Public Function GenerateReport (ByVal ReportDate As Date, ByVal LOF As String, ByVal Funding As String, _ ByVal SubFunding As String) As Variant Dim oPDF As ActiveReportsPDFExport.ARExportPDF
' create Active Report designer objects Set oRpt = New rptPerfStds oRpt.Printer.Orientation = ddOLandscape 'Go build the report with the oRpt object! 'After that oRpt.Pages.Insert intPageCount, YourReport.Pages(i) 'export to pdf Set oPDF = New ActiveReportsPDFExport.ARExportPDF ' pdf and tmp will be in current folder: oPDF.FileName = modGenReporting.GenerateTempPDF(App.Path) oRpt.Export oPDF
'create byte file for output report GenerateReport = modGenReporting.StreamFile(oPDF.FileName)
'kill pdf file and temp file modGenReporting.DeleteTempPDF oPDF.FileName End Function
A big step after creating the report is preparing the report for export which requires a temp file. This function:
modGenReporting.GenerateTempPDF(App.Path)
...is a little complex, so lets show how the temp file is made:
Public Function GenerateTempPDF(strPath As String, Optional strPrefix As String = "tmp") As String Dim strTempName As String
strTempName = GenTempName(strPath, strPrefix) If UCase(Right(strTempName, 4)) = ".TMP" Then GenerateTempPDF = Left(strTempName, Len(strTempName) - 4) & ".pdf" Else Err.Raise vbObjectError + GEN_REPORTING_ERR_UNEXPECTED_EXT, "GenerateTempPDF", "Unexpected file name format" End If End Function
Public Function GenTempName(strPath As String, Optional strPrefix As String = "tmp") Dim lngUnique As Long Dim strTempFileName As String lngUnique = 0 strTempFileName = Space$(100) GetTempFileName strPath, strPrefix, lngUnique, strTempFileName If Trim(strTempFileName) = "" Then strTempFileName = "" Err.Raise vbObjectError + GEN_REPORTING_ERR_NO_TEMP_FILE, "modGenReporting.GenTempName", "Error creating temporary file." Else strTempFileName = Mid$(strTempFileName, 1, InStr(strTempFileName, Chr$(0)) - 1) End If GenTempName = strTempFileName Exit Function End Function
Private Declare Function GetTempFileName Lib "kernel32" Alias "GetTempFileNameA" (ByVal lpszPath As String, ByVal lpPrefixString As String, ByVal wUnique As Long, ByVal lpTempFileName As String) As Long
Ok, now to stream the file to the .ASP page for display with function:
modGenReporting.StreamFile
We use this:
Public Function StreamFile(ByVal strFileName As String) On Error GoTo ErrHnd Dim lngFlen As Long Dim bytStream() As Byte Dim strBinFile As String Dim intFHan As Integer
Open strFileName For Binary Access Read As #intFHan
Get #intFHan, , bytStream
Close #intFHan
StreamFile = bytStream
Exit Function End Function
Then, set the stream to the return value the function: GenerateReport = byte stream.
Compile and register your COM object on the Web Server.
Lastly, call the COM within your ASP page: (ASP VBS Server code)
'get report info from query string strOfficeId = Request.QueryString("office") strFundingSource = Request.QueryString("FundSource") strFundingSubCat = Request.QueryString("FundSubCat") 'instantiate the report with whatever class name you gave it set oRpt = Server.CreateObject("WIAPSServer.cWIAPerfStds") 'set up the HTTP Content-Type, and write out the report in binary Response.ContentType="application/pdf" Response.BinaryWrite oRpt.GenerateReport(CDate(Now),strOfficeId,strFundingSource,strFundingSubCat)
The browser will respond by opening Adobe Reader to display the output. If the Reader is not installed you will be prompted to download it. This method of displaying reports should work for either Netscape or IE.
Questions can be directed to burnettm@hotmail.com, but I don't promise to answer them ( ;o) ). However, the reference to Data Dynamics is a good one and I would use that first.
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.