Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Applets
Expertise: Beginner
Mar 19, 1997

Printing from Applets

Question:
I am trying to construct an applet that accepts data from the user and performs several calculations on that data. I would like to give the capability to print out the results of the calculation in a form. The template for the form will reside on the applet's server. Is there any way to take the information given by the user and the results of the calculation and insert them into the form, and then print the form to the users local printer? I was thinking that I could implement a client/server to do this but am not quite sure about this.

Answer:
Java does not provide any support for printing at this point. Printer access may be provided in future releases of Java but there is no standard platform-independent way to print from Java applets or Java applications today.

This probably has as much to do with the multitude of printing software and printers out there as it does with the many ways operating systems handle printing. If Java were to support one, it would have to support all of them in order to remain truly cross-platform.

Even if there were a way to print from Java, applets probably wouldn't be allowed to print simply because applet access to local devices and files is strictly forbidden for security reasons. Imagine a malicious applet that started printing garbage to the user's local printer as soon as it was loaded from the Net. It might steal fonts out of your printer, interfere with the existing print jobs, or print tons and tons of annoying advertisements without your knowing where they came from.

You might be able to create a standalone Java application for printing to a particular device on a given operating system, but it's sure to be platform dependent. For example, if there's a way to get to the parallel or serial port on your system through a file name, say /dev/ttya on UNIX systems, or lpt1 on wintel systems, you can certainly open a connection to the printer through the parallel or serial port file name and feed the text or postscript data to it, but there is no way for the app to know if it's talking to a printer or to a modem. Here's how that might look in Java:

        import java.io.*;

        PrintStream printer = new PrintStream(new FileOutputStream("/dev/ttya"));
        for (int i = 0; i < lineCount; i++) 
                printer.println(line[i]);

DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date