Print current html page on printer from java bean in jsf

一笑奈何 提交于 2019-12-11 02:39:51

问题


i need to change javascript window.print() command with java backing bean process.

here, i am using Jsf1.2

as programmatic, print any web page,

we use window.print(). which opens one menu to select printer to print page.

now i need to compress this process with java bean method call.but it not works for me anyhow.

what i actually need is, when i click on print button on webpage. it directly print on selected printer(this printer selection done in backing bean with PrintServiceLookup.lookupPrintServices method. this selection works proper for me.)

here is my question is how to get my full page content in java bean on button click for print.

and one more problem here,

when i do simple string print with below code it show no error in code,at compile time and run time but show error while printing on printer(below image). i get this code from online research i am using network printer.

printing code::

try{
            System.out.println("getHtmlData = "+getHtmlData());
            PrintService[] printServices;
            String testData = "Hello World my first java print";
            InputStream is = new ByteArrayInputStream(testData.getBytes());
            DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
            PrintServiceAttributeSet printServiceAttributeSet = new HashPrintServiceAttributeSet();
            System.out.println("getPrinter() = "+getPrinter()); // here i give my printer name
            printServiceAttributeSet.add(new PrinterName(getPrinter(), null));       
            printServices = PrintServiceLookup.lookupPrintServices(null, printServiceAttributeSet); 
            System.out.println("printServices len = "+printServices.length+" ::: name = "+printServices[0].getName()); ///to verify its my selected printer or not
            PrintService service = printServices[0];
            DocFlavor[] flavors = service.getSupportedDocFlavors() ;
            //display : selected printers flavors
            for (int i = 0; i < flavors.length; i++) {
                 System.out.println("\t" +flavors[i]);
            }
            Doc doc= new SimpleDoc(is, flavor, null);
            DocPrintJob job = service.createPrintJob();
            //PrintJobWatcher pjDone = new PrintJobWatcher(job);

            // Print it
            job.print(doc, null);
            is.close();
            System.out.println("print Done");  
        }catch (Exception e) {
            System.out.println("error 1 "+e.toString());
           e.printStackTrace();
        }

i know this sound like spying process but i need to print page from java bean.

update 1:

  • i can get page content with document.documentElement.outerHTML in js(actually i dont want to use js for passing html content.this can be last option to do if not found anything).and then pass it to bean from inputhidden. but now how to use this String content for print.

来源:https://stackoverflow.com/questions/15810865/print-current-html-page-on-printer-from-java-bean-in-jsf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!