aspose file tools*
The moose likes Servlets and the fly likes Printing in servlets? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Printing in servlets?" Watch "Printing in servlets?" New topic
Author

Printing in servlets?

Aman Rustogi
Greenhorn

Joined: May 11, 2001
Posts: 22
hi,
can I have the facility of printing documents from Servlets? I need to provide a PRINT option in HTMl pages generated from servlet?
How do I go about doing this? plz guide me
thnaking u in advance,
cheers
Aman
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16065
    
  21

It sounds like you want the client machine to be able to print. Servlets run on the server machine, so they can't use the client machine's printers - not only is the whole http protocol foreign to that process, but the server machine doesn't know anything about the client's printers anyway.
Making a client print using servlets is no different than it would be for any other way of producing web documents. You can return output in a custom format like PDF or MS-WORD, or you can return straight HTML - in either case, the user has to manually select "print" from the file menu (or you have to do something like setup a "print" button on the HTML that's tied to a Javascript that does printing).
You can take a more active role in printing by embedding a Java applet on the HTML page, but it would have to be signed, since Java's sandbox rules forbid applets from printing otherwise.


Customer surveys are for companies who didn't pay proper attention to begin with.
Sunetra Saha
Ranch Hand

Joined: Feb 25, 2001
Posts: 77
Maybe you can try embedding a javascript print function in the html thrown by the servlet.
Aman Rustogi
Greenhorn

Joined: May 11, 2001
Posts: 22
thx all!!
but the solution provided dusnt serve my purpose.
The javascript print option doesnt allow us to select the file to print and besides it isnt very reliable and also doesnt work in DOM!!
So I've designed my servlet in a manner in which it first displays a file dialog box enabling user to select the file and then submitting the data which will invoke another/same servlet, get an instance of PrinterJob, set the required attributes, have an inner class implementing Printable interface thus overriding Print(), draw the contents of the requested file to the Graphics object and then print the contents.
How does it sound? even i'm not sure of it, still working on it!
Thx all for ur support!
cheers
Aman
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16065
    
  21

Sounds like you're on the right track.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I see you know the print package very well,
so please can you e-mail some simple code or plain Instructions
how to print a plain java/swing component, since I can't get it
working,
It will be very helpfull,
yastern@netzero.net
ramesh palagiri
Greenhorn

Joined: Jul 31, 2001
Posts: 1
Hi Aman,
Can you mail to me how I can print through the servlets on the client side, if possible are there any other ways to do.
I am trying to generate reports that should be printer on the client who will press a button like Print Report on his machine
on clicking the button a servlet is invoked that will get the data from the database and it should throw it in a formatted format on the client's printer.
thanks,
p ramesh babu
maimailto:Prameshb@hydbad.tcs.co.in
Raghavendra Holla
Ranch Hand

Joined: Jun 02, 2000
Posts: 58
Still servlet runs at server, and can access network printers connected to it. But you can't print on specific client printer. If you can do that, that over rides simple web security principle. In web client should initiate the action, and authorise use any local resources. Just selecting a file won't give you that permission. Any way check it. Even I am interested. if you succeed post your solution here. bye.
regards.
Raghavendra Holla.
Aman Rustogi
Greenhorn

Joined: May 11, 2001
Posts: 22
Hi,
Thanking u'll again for all replies and queries. I'm still in development stage of the code to print files to the printer. The sooner I'm thr' with it, I shall publish it here! Currently i'm encountering an error while drawing the contents of file to be printed to the graphics object using drawString (). Calling this function, crashes the HotSpot Virtual Machine. Working 'round for a feasible solution, shall keep u updated. I guess the problem is using post-script printer drivers or with data-format sent to the printer - not sure yet!
Raghavendra, as u rightly said that i cannot access/print to clients printer using servlets, but i need this option in my intranet/interner site which will be accessible to just ONE SINGLE USER!
cheers
Aman
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Printing in servlets?