客户端打印机上的javascript打印
我正在用Java开发web应用程序,当点击按钮时,我必须在客户端打印机上打印报告。如何实施?如何从客户端显示打印机
我使用:
PrintService[] printers =
PrintServiceLookup.lookupPrintServices(null, null);
但这是在服务器端
你可以在下面搜索框中键入要查询的问题!
我正在用Java开发web应用程序,当点击按钮时,我必须在客户端打印机上打印报告。如何实施?如何从客户端显示打印机
我使用:
PrintService[] printers =
PrintServiceLookup.lookupPrintServices(null, null);
但这是在服务器端
您必须在客户端页面上使用javascript
window.print()
https://developer.mozilla.org/en-US/docs/Web/API/Window/print
如果您想尝试applet方法,请看一下this answer
You cannot do that for security reasons. If you could, applets would already have become notorious for printing 10+ pages of 'special offers' when you visit unscrupulous web sites.
OTOH, if the client is willing to accept one prompt at applet start-up, you could digitally sign the code.
此外,使用来自JNLP API的PrintService也可以获得类似的结果,而不需要签名的小程序
就像下面的例子一样
import javax.jnlp.*;
...
PrintService ps;
try {
ps = (PrintService)ServiceManager.lookup("javax.jnlp.PrintService");
} catch (UnavailableServiceException e) {
ps = null;
}
if (ps != null) {
try {
// get the default PageFormat
PageFormat pf = ps.getDefaultPage();
// ask the user to customize the PageFormat
PageFormat newPf = ps.showPageFormatDialog(pf);
// print the document with the PageFormat above
ps.print(new DocToPrint());
} catch (Exception e) {
e.printStackTrace();
}
}
// Code to construct the Printable Document
class DocToPrint implements Printable {
public int print(Graphics g, PageFormat pageformat, int PageIndex){
// code to generate what you want to print
}
}
# 1 楼答案
因为你的要求不清楚。我不确定您到底想要什么,但对于打印,您可以使用此
window.print()
你可以阅读更多关于这个here(简单)和here(解释)
编辑: 如果要打印颗粒元素内容,可以使用此功能: