• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Printing in servlets?

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Saloon Keeper
Posts: 27808
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe you can try embedding a javascript print function in the html thrown by the servlet.
 
Aman Rustogi
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27808
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sounds like you're on the right track.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic