-1

I am developing web application in Java, and I must print reports, when clicked on button, on client side printer. How to implement? How to show printers from client side???

I use:

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

But this is on server side.

Landre
  • 51
  • 2
  • 7

2 Answers2

3

As your requirement is not clear. I am not sure about what exactly you want but for print you can use this window.print()

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

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

You can read more about this here(simple) and here(explained).

Edit: And if you want to print a particulate element content you can use this function:

 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 ;
}
Anand Singh
  • 2,343
  • 1
  • 22
  • 34
1

You must use javascript on the client page.

window.print()

https://developer.mozilla.org/en-US/docs/Web/API/Window/print

If you want to try the applet approach give a look at 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.

Also it should be possible to achieve a similar result using the PrintService from JNLP API without the need for a signed applet.

Like in the Following example

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   
        }
    }
Community
  • 1
  • 1
Bolza
  • 1,904
  • 2
  • 17
  • 42