有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

客户端打印机上的javascript打印

我正在用Java开发web应用程序,当点击按钮时,我必须在客户端打印机上打印报告。如何实施?如何从客户端显示打印机

我使用:

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

但这是在服务器端


共 (2) 个答案

  1. # 1 楼答案

    因为你的要求不清楚。我不确定您到底想要什么,但对于打印,您可以使用此window.print()

    &13; 第13部分,;
    function myFunction() {
        window.print();
    }
    <p>Click To print.</p>
    
    <button onclick="myFunction()">Click</button>
    和#13;
    和#13;

    你可以阅读更多关于这个here(简单)和here(解释)

    编辑: 如果要打印颗粒元素内容,可以使用此功能:

     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 ;
    }
    
  • # 2 楼答案

    您必须在客户端页面上使用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   
            }
        }