Currently, the Netscape and Microsoft browsers do not support the direct printing of HTML documents that contain any type of Java applet, including NetCharts. As a service to NetCharts customers, we have developed a collection of capabilities that allow either browser users or server administrators to generate printable versions of HTML documents containing any Java applet. (Naturally, we'd like to believe that you only want to print NetCharts applets, but if you have other applets you can print those as well.)
Printing Java applets via NetCharts is accomplished through the generation of "snapshot" documents, in which all Java applets are replaced by GIF images generated directly from those applets. Since Netscape and Microsoft browsers can print GIF files, this allows you to print a document with an image of each Java applet.
Snapshot processing is accomplished with the following classes:
The Snapshot Applet and Server are provided to allow browser users to generate a snapshot of any document containing one or more applets. The Snapshot Applet appears as a button, which generates a snapshot of the current document when pressed. That is, if you include the Snapshot Applet in a document, then a snapshot can be generated for that document. Since we don't have access to all of the necessary browser features (e.g. the print utility), the Snapshot Applet works in the following, slightly round-about way. (But remember, at least it works.)
When the Snapshot Applet button is pressed, the Snapshot Applet queries the browser to determine the current HTML document, which it then retrieves from the Web server. For all static HTML documents, this is a straight-forward process. For CGI-generated HTML documents, there are some other issues to be concerned with, which are covered below.
Once the document is retrieved, it is parsed to determine all of the NAMED applets contained within it. That is, it looks for all applets that have a value assigned to the NAME attribute in the APPLET tag. For example, the following document contains a simple piechart, named "piechart" and a Snapshot Applet, named "snapshot".
<h1>NetCharts Snapshot Example</h1> <applet name=piechart codebase=/NetCharts/classes code=NFPiechartApp.class width=300 height=300> <param name=NFParamScript value=' Slices = (10), (20), (30); '> </applet> <br> <br> Press the button to generate a snapshot <br> <br> <applet name=snapshot codebase=/NetCharts/classes code=NFSnapshotApp.class width=200 height=100> <param name=NFParamScript value=' ServerName = "webserver"; ServerPort = 1234; TargetWindow = "SnapshotWindow"; '> </applet>
For each NAMED applet found, the Snapshot Applet generates a GIF file containing the current displayed image of that applet. It also replaces the APPLET tag in the HTML document, with an IMG tag that references the newly created GIF file.
Unfortunately, browsers won't let Java applets store files locally (because of security restrictions), so the Snapshot Applet needs to store those GIF files back on the Web Server. That is where the Snapshot Server comes in. The Snapshot Server's sole job is to wait for connections from one or more Snapshot Applets and to deposit their converted HTML and GIF files in a temporary directory on the server. Only one Snapshot Server is needed to service an entire army of Snapshot Applets, in conjunction with a Web server. (The Snapshot Server is a compact, multi-threaded server that demonstrates Java's massive potential as a custom server development environment.)
Once all of the Applets have had their pictures taken, the Snapshot Applet instructs the browser to retrieve the snapshot of the HTML document from the temporary Web server directory, and display it in another frame or window. At that point, the user can modify the display format as allowed by Netscape and Microsoft, prior to printing it using their menu options. Since the snapshot document contains only GIF images at this point, the browser can handle the printing chore.
When the Snapshot Applet disconnects from the Snapshot Server (which happens when the browser overwrites the document with another document or when the browser is iconified) all of the temporary files are automatically deleted to prevent Web server storage problems. Nonetheless, it is good practice to periodically prune the temporary Web server directory to prevent resource overflows because of a failure of the automatic heuristics.
The process described above works well with statically defined HTML documents, but requires a little bit of care when the document being processed is dynamically generated from a CGI script. In that case, the Snapshot Applet needs to be able to re-generate the document, in order to generate the snapshot. If, however, the CGI script was invoked using the POST method of an HTML form, then the necessary input parameters will not be available to the Snapshot Applet to re-generate the HTMl document. (Java applets cannot use the POST method, because of security restrictions.)
Therefore, if you want to use the Snapshot Applet with CGI-generated documents, then you must use the (uglier) GET method when invoking the CGI script. (It's uglier because it appends all of the input parameters to the end of the URL and the user gets to see all kinds of internal guts.) Sorry, but we have found no way to get around this problem.
Although the Snapshot Applet/Server combination gets the job done, it requires the user to press a button, wait a little bit, see the apparently same document re-appear in another window and then select File-Print from the browser menu. (The fact that it works is its only saving grace and we will retire it as soon as the Netscape or Microsoft browsers integrate Java applets with their own document printing capabilities.)
In those situations where such end-user acrobatics are unacceptable or where the charts only change occasionally, the Snapshot Utility can be used to generate snapshot files on the server itself. It that way, the user can be given the GIF files in the first place, which are readily printed. This approach also supports the dynamic generation of charts if necessary, with the only difference being that the Java processing is done on the server, not on the browser. (Clearly, since the user sees only GIF images, they also don't get the benefit of the great interactive capabilities in NetCharts.)
The Snapshot Utility is a stand-alone Java application that combines the essential capabilities of the Snapshot Applet and the Snapshot Server. The Snapshot Utility is executed from the command line, and is given the name of one or more HTML files, and an optional output directory.
Each HTML document is processed in exactly the same way as the Snapshot Applet and a collection of one or more GIF files are generated, as well as a snapshot HTML document. All files are named similarly and are stored in the designated output directory. At that point, you can either store them someplace in the Web server for permanent reference, such as would be done for the monthly status reports, or they can be used immediately by another script to service some information request. This latter approach is especially useful for systems where the browsers don't support, for whatever reason, Java applets and the developers still want to use NetCharts to handle their graphics needs. (After all, why use silly old text-based barcharts, when you can use hundreds of colors and neat graphics?) In essence, HTML documents containing NetCharts parameters are used as templates for the CGI scripts that generate HTML documents containing GIF images upon request from the user. The Snapshot Utility can easily by used by any script to perform intermediate processing.
The only restriction on the Snapshot Utility is that it must be executed from within the Windowing environment on the server. For Windows, this is not a problem, because everything executes within the windowing environment. However, in a Unix system, where scripts can execute on a server without any windowing system running, this is a problem because Java needs the windowing system to perform graphics operations. (This deficiency in the basic Java image processing capabilities will hopefully be fixed in the near future.)
The Snapshot Utility is also useful for generating GIF images of charts for inclusion in other presentations or documents. Most of the chart examples shown in the NetCharts documentation were generated using the Snapshot Utility. With a little bit of pre-processing, applet parameters can be "massaged" prior to calling the Snapshot Utility in order to create snapshots of any width and height. (Such a feature will probably be added to the Snapshot Utility itself in the future.)
If the current web browsers supported printing of Java applets, the Snapshot Applet and Server wouldn't be needed. (The Snapshot Utility is useful for other reasons and would have been built anyway.) But, they were developed to help the Java community print documents containing applets, especially charts. They don't represent a perfect solution, but they do represent a workable solution. We can only hope that Netscape and Microsoft and other browser vendors provide intrinsic printing of Java applets in the near future.