Automatically printing on clients printer using servlets

I need to make a servlet that allows the user to click a button on my site and it starts printing a bunch of documents on my server on his printer. Is this possible? The documents could be images, pdf, etc. My boss wants one click to print all the docs.
[267 byte] By [jerkyboy] at [2007-9-26 3:34:40]
# 1
Hi,It is not possible to do the thing with only a servlet and a browser....However, you can write a RMI or Socket Client(if you want to use the browser, the client can be an applet) to achive the same.
shubhrajit_c at 2007-6-29 12:04:17 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Thanks for the response, I was pretty sure it couldn't be done, but I wanted to make sure. I'll look into the Applet route.

I haven't wrote Applets before, but isn't it true that IE5 comes with Java disabled by default? So if I write this Applet most of my clients couldn't use it without turning on Java on there browser?

jerkyboy at 2007-6-29 12:04:17 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

I don't think so. Anyway even it is disabled you will have to instruct your clients to enable it. This won't be difficult as I feel you will develop an intranet based application.

One more thing I forgot to tell you. Even I did not do a lot of development with applets(I am a JSP guy), so I am not sure if the applet can access the local printer with the default security manager. Probably you will have to install a custom security manager for the client machines as well.

If you don't mind, I would say that your requirement is rather weird !! Why on earth should the server force to print a bunch of documents on the client machine ? If the client requires a document he can always print it !

shubhrajit_c at 2007-6-29 12:04:17 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

Hello

Applets cannot directly access printing services on the client's local machine. However Signed Applets ,with proper permissions , can access printing services.

In order to print Images, rtf docs, pdf docs directly from a page with just one button click, signed applet is the best solution from a Java Technology point of view. You may have to use printing APIs provided in java.awt.* package or java.awt.print.* package. However there is one wrinkle in using java.awt.print.*. Apparently this package is from Java 2 and IE may not fully support this package. So I would suggest in using java.awt.* package classes.

From a security purposes you may have to use both Netscape's Security APIs and Microsoft APIs to assert permissions for printing. For example you can assert permission as follows:

boolean cblnIsNetscape = System.getProperty("java.vendor").substring(0,8).equals("Netscape");

if( !cblnIsNetscape && Class.forName( "com.ms.security.PolicyEngine") != null )

{com.ms.security.PolicyEngine.assertPermission( com.ms.security.PermissionID.PRINTING );

}

if( cblnIsNetscape )netscape.security.PrivilegeManager.enablePrivilege("UniversalPrintJobAccess");

In order to communicate with the server, you may have to implement Applet-Servlet communication with a Applet receiving the image object or doc data in byte[] format that is serialized by a Servlet into the ioStream using ObjectOutputStream. You can design the architecture according to your requirements.

Once you get Image object or doc data, you have to render this onto a container like panel or textPane without actually displaying the container to the user. Ths is required because printing object gives you a java.awt.Graphics object on which the data needs to be renderred. So in anyway you have to render the data on a container. You can refer to java.awt.* apis docs for more information on printing using awt components or swing components.

One more issue regarding signed applets. Netscape accepts jar files for signed applets but IE does not recognize signed jar files. IE reads signed cabinet(cab)files. The files can be created using Microsofts Java SDK that can be downloaded from Microsoft site.

The cab file location should be specified as a param to the applet like

<param name="CABBASE" value="mydir/myapplet.cab">

or

<param name="CABINETS" value="mydir/firstapp.cab,mydir/secondapp.cab">

The following site contains detailed info regarding signing applets for both IE and Netscape:

http://www.suitable.com/Doc_CodeSigning.shtml

Swing provides some apis to render rtf documents. I think there are some apis that are available free that can render pdf files too.

Hope that gives some input to your query.

Kashabm

kashabm at 2007-6-29 12:04:17 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

Wow, thank you for all the input. Looks like I got some work ahead of me.

I thought this requirement was pretty off the wall myself, but my boss was pretty insistent that this be done. This isn't even an intranet project, this if for our main site.

I guess I'll be an applet master my the time I get done with this.

Thanks

jerkyboy at 2007-6-29 12:04:17 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...