我正在用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
如果你想嘗試小程序的方法給看看this answer
你不能這樣做,出於安全考慮。如果可以的話,當你訪問不道德的網站時,小程序 已經因打印超過10頁的「特殊 優惠」而臭名昭着。
OTOH,如果客戶願意在小應用程序 啓動時接受一個提示,您可以對代碼進行數字簽名。
另外它應該可以使用來自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
}
}
根據您的要求不明確。我不知道你想要什麼,但對於打印您可以使用此window.print()
function myFunction() {
window.print();
}
<p>Click To print.</p>
<button onclick="myFunction()">Click</button>
編輯: 如果你要打印的顆粒元素內容,您可以使用此功能:如果您需要打印的客戶端,那麼你需要使用`的JavaScript window.print(
function myPrint(data)
{
var testPage = window.open('', 'Test Page', 'height=500,width=500');
testPage.document.write('<html><head><title>Test Page</title>');
testPage.document.write('</head><body >');
testPage.document.write(data);
testPage.document.write('</body></html>');
testPage.document.close();
testPage.focus();
testPage.print();
testPage.close();
return ;
}
)'方法。 – androidGenX
我用Java開發,我必須打印一些報告@androidGenX – Landre
嘿,如果我的回答是正確的,讓它接受。 –