Link to home
Start Free TrialLog in
Avatar of koala1998
koala1998

asked on

Java applet accessing printer?

Hi,

Is it possible for a Java applet to access a default local print of my web browser?

I am trying to write a Java applet directly print some report(may be text + graphics) to the default printer of my web browser.

Is it possible to do it now with JDK1.1.X?
Avatar of Mukund
Mukund

Yes. You can use the new printer object in JDK 1.1 to display all of your screen output in the printer.  You basically use the printer object instead of the graphics object in your paint module. You need to remember to flush each page (no default form feed).  
Avatar of koala1998

ASKER

Okay, but may be I did not specify very clearly in my question.  What I wanted is, to provide the user with a list windows, in which listed are all the printable document from my server.  

What I wanted to do is after the user has selected a certain document, he can press a "print" button, and that docuemnt will be automatically loaded from the server and be printed to a default printer.

Can Java achieve that?
Sure, you could write an applet to display the list of
printable documents, and I response to the user's button press
you could print the appropriate file to his printer using the
aforementioned printer object.

You will need to sign your applet in order to obtain the security
permissions needed to print to the user's printer.  Both Netscape
and Microsoft do it differently, so you will need to bits of code and 2 digital IDs.  In addition, since the Netscape capability API is not present in the IE Java implementation, you will need to do browser detection and only load the appropriate
printing code for each browser.  It's possible and I've done it, although I was printing the contents of a graphically rendered AWT Frame, not a text document.

Depending on your time budget you may want to just display the server documents in a new browser window, and let the browser print them, when the user presses the print button.
ASKER CERTIFIED SOLUTION
Avatar of Brill
Brill

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
Thanks for your response.  But one more question is where can I obtain those two Digital ID?  And what is that "bits of code"?
There doesn't seem to be a way to edit or delete an answer I have already submitted, but I thought I'd expand on what I'd already entered. So, this is the answer that should be up top

Yes. However, you will have to sign the applet in order to give the applet access outside the browsers sandbox. Both Netscape & IE require a different certificate. Both the Microsoft and Netscape sites have extensive information on how this is done. If you are signing for another browser, you'll have to find out first if the browser supports signed applets, and how the certificate can be applied.

The two methods I have used/seen to allow an applet to run in both Netscape & IE, is to implement a 'do-nothing' security class, or catch ClassNotFound exceptions. In the 'do-nothing' case the Browser will load its own version of the class before it tries to load the 'do-nothing' version. What that means, is that in all situations, the applet is requesting access for both browsers... the one that matters will be the one granting the access. You can also catch ClassNotFound exceptions. This is not the prefereed way to do it however, becase the Web server the applet comes from will get a 404 (File Not Found) error logged to its log. (this happens becase the VM will try to find the class file on the server if its not in the system, or the jar archive).
An example of both the Netscape and IE 'do-nothing' classes follows:

The MS do-nothing classes (there are two required)
-------------------------------------
ackage com.ms.security;
lass PermissionID


-------------------------------------
ackage com.ms.security;
ublic class PolicyEngine

public static void assertPermission( PermissionID p){
      // do nothing!!!  This class will be loaded from Microsoft's system class
      // if and only if we are running in the Explorer VM!!!
      return;
}

public static synchronized PermissionID permissionNameToID(String pname){
      return null;!
}

-------------------------------------
And the Netscape class.
-------------------------------------
ackage netscape.security;
public class PrivilegeManager

public static void enablePrivilege( String priv){
      // do nothing!!!  This class will be loaded from Netscape's system class
      // if and only if we are running in the Netscape VM!!!
      return;
}