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

Print a file to a printer

Sree Harsha Chintamani
Greenhorn

Joined: Oct 29, 2013
Posts: 9



I'm trying to print a file
Unable to find where the error is
can anyone suggest me where my code isn't working
my printer on the network is Canon MP280 series Printer
i'm able to see it in list of printer,but clicking on print doesn't print my file
Thanks in advance
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42950
    
  72
If this is a servlet, what are you clicking on? Does the server not run in headless mode?
Sree Harsha Chintamani
Greenhorn

Joined: Oct 29, 2013
Posts: 9
I Placed the above code in a method,
on invoking this method i need it send the file to printer for printing
Sree Harsha Chintamani
Greenhorn

Joined: Oct 29, 2013
Posts: 9
server does not run in a head less mode,
i'm able to print to a printer directly connected to system,
my C:\WINDOWS\system32\spool folder does show the files i tried to print
unable to do that for a network printer
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42950
    
  72
But what good is a print dialog on a server?
Sree Harsha Chintamani
Greenhorn

Joined: Oct 29, 2013
Posts: 9
i'm trying to print a file with details from a database as per the user query.
when user clicks on print he has to get the option to select a printer from the list of printers attached to his machine,so i'm using printDialog.
once user selects the printer the files should go to the printer for printing
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42950
    
  72
Servlets run on a server - the user will never see any print dialogs. You need to find a different solution.
Sree Harsha Chintamani
Greenhorn

Joined: Oct 29, 2013
Posts: 9
Sorry,Earlier i was not very clear
Right now i'm posting my real scenario

I'm Working on a struts web application where the user wishes to print the data which is saved in file which contains details from database(mysql) as per his query.I'm placing the above code in a method and when this is invoked it gives a print dialog for user where he can select the printer from list of printers attached to his machine. If i try to print to a local printer the files are being sent to C:\WINDOWS\system32\spool folder and goes for printing. but it does not happen in the case of a network printer my printer on the network is Canon MP280 series Printer i'm able to see it in list of printer,but unable to print my file
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42950
    
  72
Yes, I understood all that. None of it changes what I wrote in my last post - this is fundamentally impossible. If you think it is, then you do not understand how web apps work - most importantly, that its Java code is executed on the server, and not on the client.

If you want to print in a web app, either create an HTML page that the user can print from within the browser, or create a file on the server that gets downloaded to the client (where the user can do anything he wants with it, including printing it).
Sree Harsha Chintamani
Greenhorn

Joined: Oct 29, 2013
Posts: 9
First let me say thanks for your patience and time.
the file is getting downloaded to the clients location.
i'm trying to print the same file to a printer

argument file in my first line of code contains the path to the location where the file is
client does not wish to go to location of the file and print it manually,so i'm using the above method
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42950
    
  72
It seems I'm not getting the main point across: this approach is broken in a very fundamental way that can not be fixed. Are you maybe confused because during development you're running the servlet container on your desktop machine, and both client and server machine are identical? That will not be the case for the deployed web app, though.

argument file in my first line of code contains the path to the location where the file is

That's a file on the server - the user does not have access to it.

client does not wish to go to location of the file

He can't, see above. The only way for the client to print a file is to stream it to the client browser, where the user can decide whether to open or save the file.
Sree Harsha Chintamani
Greenhorn

Joined: Oct 29, 2013
Posts: 9
With your previous post i'm very clear now
earlier i was confused as both the client and server reside in the same machine.
is there any way to print the downloaded file directly with out his intervention
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42950
    
  72
No, that's not possible. Imagine the security implications if some random web site you visit was able to print stuff on your local printer without you knowing about it!

But even if it was possible, I would question this approach. Why not let the user be in charge? Maybe she doesn't want to print the file. Maybe she'd prefer to keep it as a file. Maybe she wants to print it later, on some printer that isn't accessible right at that time. User empowerment is a good thing.

client does not wish to go to location of the file

I don't know what kind of users you need to work with, but it seems most folks have no problem grasping the concept of a "Download" folder where all files downloaded by the web browser go.
sachin naikwadi
Greenhorn

Joined: Oct 22, 2013
Posts: 7

Ulf Dittmer is right, their is no way from java servlets to facilitate user to print file on their local machine.
but still if their is any workaround available then it has to be some sort of javascript plugin or api which whenever required will run on client machine.
But again I doubt how much browser will support this type of functionality as obviously it creates a security issue.


Regards,
Sachin
 
 
subject: Print a file to a printer