My employer recently tasked me with providing access, via the Internet, to approximately 30 million DB2 UDB records stored on an AS/400. Specifically, I needed to create an interactive, Web-based reporting system. This system would have to display the results in a browser as well as email the results to the requesting client. The platform for this project was Microsofts Internet Information Server (IIS) and Active Server Pages (ASP). I decided to use Seagate Softwares Crystal Reports for this project because I have extensive experience with Crystal Reports and it has Web-reporting capabilities. Ultimately, I chose a solution in which a PostScript file, created by Crystal Reports, is converted to Adobes PDF. In turn, that PDF file is displayed in the clients browser and emailed to the client. However, before that solution was implemented, I explored Crystals Web-reporting capabilities.
The first Web-reporting capability I investigated was SmartViewer. Its a browser plug-in, which comes in two flavors, ActiveX and Java. However, I quickly discovered SmartViewer had some problems. The Java version produced very degraded graphics when displaying in Internet Explorer. This was caused by Microsofts method of scaling images. (For a full explanation of this problem, visit the Seagate Knowledge Base page at community.seagatesoftware.com/library/kbase/articles/ c2002974.asp.) Additionally, the ActiveX version sometimes had problems displaying an entire report created in landscape mode.
Finally, SmartViewer cannot generate a file that could be subsequently emailed. Another option I explored was Crystals exporting features. Crystal Reports is capable of exporting to many popular formats such as Word, Excel, and HTML. However, none of these formats would identically reproduce the report as it was shown on the screen. I then turned to delivering these reports using Adobes PDF.
I had ruled out SmartViewer and Crystals exporting. That left one option: Print the report to a file. The concept is pretty simple. Print the report to a file, convert that file to PDF using the Adobe Distiller program, display the PDF file in the browser, and email the PDF to the client. However, as with most programming tasks, especially ones that are Web-based, implementing this project turned out to be elusive and frustrating. Yet once Id solved the mystery of printing to a file, putting the pieces together was a snap. Here are the steps necessary to make this work:
1. Install a PostScript printer and redirect its output to a file.
2. Install Adobes Acrobat and Distiller products.
3. Modify the IISs registry to allow Web processes (specifically ASP) access to the PostScript printer.
4. Create a Crystal report using Crystals Reports Designer Component, which, for those who dont already have it, is available free from the Crystal Reports Web site at www.
seagatesoftware.com/products/crystalreports/rdc.asp for anyone with a Visual Basic
5. Write a Visual Basic Component Object Model (COM) object, which causes Crystal Reports to produce a PostScript file and causes Adobes Distiller to convert that file to PDF.
6. Write an ASP script that invokes the COM object.
Before you can replicate my solution, you need to install a PostScript printer on your server, name it POSTSCRIPT (youll see why), and configure it to print to a file. A PostScript printer is necessary because Adobes Distiller can only convert PostScript files to PDF. Redirecting the port to a file will provide that file. See the sidebar Adding a PostScript Printer and Configuring Its Port to Send the Output to a File for step-by-step instructions on how to create a PostScript printer and redirect its output to a file. Next, install Adobe Acrobat and Distiller. Now, run the Distiller program, configure the job options, and save the job options in a file. The ASP script that creates the PDF file will use these job options. See the sidebar Configuring the Distiller Options for step-by-step instructions on setting the job options.
Now, heres the first technical hurdle: Printing on an IIS requires that certain entries be in the registry. However, Web processes (including ASP scripts) run using the System account. The System account does not normally have any printer information stored in the registry. Furthermore, a COM object instantiated by an ASP page will also run using the System account. Therefore, you will need to modify the registry. See the sidebar Modifying the Registry for instructions. (For a full explanation, visit the Microsoft Knowledge Base page at support.microsoft.com/support/kb/articles/q184/2/91.asp.) Note that registry modification can cause serious, systemwide problems that may require reinstalling the operating system. Therefore, be careful! Follow the instructions in the sidebar exactly!
The Working COM Object
Its time to create a COM object using VB. You can download the complete code for the VB project referenced in this article from the Midrange Computing Web site (www.midrangecomputing.com/mc). For brevity, only the printReport method of the object is described here. This method is the heart of the object. It works by printing a Crystal report to a PostScript file and then converting that file to PDF. Open the project in VB and follow along as I describe what the PrintReport method does.
Because many instances of this component can be running at one time, the PrintReport method starts by waiting until any previous copy of a PostScript file no longer exists. The method does this to ensure that the user produces a report that is exclusive to him and that the program does not accidentally write over the output of another instance of
itself. Next, the method loops through the printer collection until it finds the printer named POSTSCRIPT. Once found, the printer and Crystals report objects are assigned to this printer so that, when Crystal is invoked, it will create a PostScript file of its output. Next, Crystals page orientation is set, and Crystals PrintOut method is invoked. The PrintOut method actually causes Crystal to read the records from the AS/400 and write the PostScript file. Finally, the PostScript file is converted to PDF by invoking Distillers FileToPDF method, and the PostScript file is deleted. Once the file is deleted, any other instances of the object will be free to process their reports.
Piece by Piece
At this point, youve created a PostScript printer, installed Distiller, safely modified the registry, and created the VB COM object that converts a Crystal Reports PostScript printout to PDF. Its now time to tie all these steps together with an ASP page created with VBScript. You can download the complete ASP and HTML files used in this example from the MC Web site. Again, for brevity, only the ASP script is described here. This RunSysCols ASP script displays certain fields from the SysColumns table of an AS/400 database. This script is called from an HTML file via a hyperlink. Here are the steps implemented in this script:
1. Create a reference to the COM object youve created and initialize the Crystal report to run.
2. Generate a random file name, which will be the final PDF file.
3. Connect to the AS/400 using ActiveX Data Object (ADO) and set some of the ADO properties.
4. Initialize the ADO recordset and pass the recordset to the COM object.
5. Using the COM object, open the Crystal report and invoke other methods used to further manipulate the Crystal environment.
6. Invoke the PrintReport object, passing in the proper parameters (report orientation, the PDF file name to create, the PostScript file name to convert, and the Distiller job options file).
7. Close the Crystal report, reset the ADO features, and display the PDF file in the clients browser.
Putting It Together!
As I stated, once the mystery of printing from an NT service was solved, putting the pieces together was a snap. The critical piece in this case was getting an ASP page to print out. Once that was done, I created a VB COM object that uses Crystal Reports and Acrobat Distiller. Finally, the ASP page tied all the pieces together. If you want to see examples of this technology in action, I invite you to visit the AdSpies.com Web site at www.adspies.com, where my company is generating reports for its users with this technique.
References and Related Materials
Crystal Reports Report Designer Component Web site: www.seagatesoftware.com/products/crystalreports/rdc.asp
Seagate Knowledge Base Web site:
Configuring the Distiller Options
1. After installing Adobe Acrobat 4.0 and Distiller, find and run Distiller. It is located under the Adobe Acrobat 4.0 item of the Programs menu.
2. Choose Settings and then Job Options from the Distiller menu.
3. The Job Options menu offers many parameters that you can use to tailor created PDFs. For example, you can configure compression, version compatibility, and color handling from within this screen. See the Distiller documentation for a more in-depth description of these parameters and how you can use them.
4. After youve configured all the Distiller options you want, press the Save As... button and save your options in a file. You might want to name your file something that you can easily identify as the options file used for your Web site.
Modifying the Registry
1. Launch the Registry Editor (Regedit.exe).
2. Select the following key:
HKEY_CURRENT_USER3. From the Registry menu, click Export Registry File.
SoftwareMicrosoftWindows NTCurrent VersionDevices
4. In the File Name text box, type c:Devices.reg.
5. Select the following key:
HKEY_CURRENT_USER6. From the Registry menu, click Export Registry File.
SoftwareMicrosoftWindows NTCurrent VersionPrinterPorts
7. In the File Name text box, type c:PrinterPorts.reg.
8. Select the following key:
HKEY_CURRENT_USER9. From the Registry menu, click Export Registry File.
SoftwareMicrosoftWindows NTCurrent VersionWindows
10. In the File Name text box, type c:Windows.reg.
11. From the Start button, select Run. Open c:Devices.reg in Notepad by typing Notepad c:Devices.reg in the Run dialog box.
12. Replace the text
HKEY_USERS.DEFAULT.13. Save the file. Then import it into the registry by double-clicking the file in Windows Explorer.
14. Repeat steps 11 through 13 for c:PrinterPorts.reg and c:Windows.reg.
Adding a PostScript Printer and Configuring Its Port to Send the Output to a File
Go to Settings/Control Panel and open the Printers icon. Select the Add Printer icon. Using the Add Printer Wizard, follow these steps:
1. First, choose My Computer as the location of the printer.
2. Proceed to the next screen and choose Add Port....
3. Next, highlight Local Port and press New Port....
4. For the port name, enter the directory and file name you want and press OK. For example, type in C:postscript.eps and press OK.
5. Now, you should be back at the Printer Ports screen. Simply close this screen and proceed.
6. Choose Next in the Add Printer Wizard screen and install any PostScript printer. I chose an HP LaserJet 5Si Mopier PS, but any PostScript printer will work.
7. Next, name the printer POSTSCRIPT.
8. You can designate this printer as not shared.
9. Finally, choose not to print a test page and click the Finish button. Youre done!