2016-03-30 273 views
-1

我正在用Java開發Web應用程序,當點擊按鈕時,客戶端打印機上必須打印報告。如何實施?如何從客戶端顯示打印機?在客戶端打印機上打印

我用:

PrintService[] printers = 
PrintServiceLookup.lookupPrintServices(null, null); 

但是,這是在服務器端。

+0

)'方法。 – androidGenX

+0

我用Java開發,我必須打印一些報告@androidGenX – Landre

+0

嘿,如果我的回答是正確的,讓它接受。 –

回答

1

您必須在客戶端頁面上使用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 
     } 
    } 
+0

我使用Java開發,我必須打印一些報告@Bolza – Landre

+0

您意識到您無法從服務器強制執行用戶打印機上的操作,是嗎?你需要做到客戶端。 – Bolza

+0

我明白了!我想用applet,解決我的問題還是javascript?@Bolza – Landre

3

根據您的要求不明確。我不知道你想要什麼,但對於打印您可以使用此window.print()

function myFunction() { 
 
    window.print(); 
 
}
<p>Click To print.</p> 
 

 
<button onclick="myFunction()">Click</button>

你可以閱讀更多關於這個here(簡單)和here(解釋)。

編輯: 如果你要打印的顆粒元素內容,您可以使用此功能:如果您需要打印的客戶端,那麼你需要使用`的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 ; 
} 
+0

我在Java的發展,我必須打印一些報告@Anand辛格 – Landre

+0

是你在一些html元素報告? –

+0

我編譯並填寫碧玉報告,我必須打印!對不起,我的英文不好! @Anand Singh – Landre

相關問題