简体   繁体   中英

Desktop.getDesktop().print(path) doesn't work in web application

I have following code in web aplication

java.​awt.Desktop.getDesktop().print(new File(path));

It is working with any problem on desktop aplication. But doens't in web app. And I need print the file. How to do that?

As Joachim has pointed out, the Java code is running on the server, not the client so you're not going to be able to use Java to print from a web-app.

What you can use though is JavaScript window.print() to print the contents of the browser window.

Now you're unlikely to want to print the window contents exactly as it appears in the browser, which is where a print style-sheet comes into play - assuming your page was written to be web-standards compliant (at least when it comes to the use of CSS), you simply write a second style-sheet that controls the appearance of the page when it's printed as described here .

So in your case, you would need to render the contents of the file into the page the browser is displaying (maybe in a hidden div ) and then the print style-sheet would hide the content displayed in the browser and show the file contents.

The printing should be done by a javascript code, if to print in the client side. If it's done in Java the printing will be done in the server side.

A printing example in java ee: http://shyarmal.blogspot.com/2011/08/printing-example-with-java-ee.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM