Printing the current html page on a printer from java bean to jsf

I need to change the javascript window.print() command using java support for a bean process.

here, I am using Jsf1.2

as a program, print any web page,

we use window.print (). which opens a single menu to select the printer to print the page.

Now I need to compress this process using the java bean method call.but, but it does not work for me anyway.

what i really need when i click the print button on the web page. it prints directly to the selected printer (this printer selection is made with bean support using the PrintServiceLookup.lookupPrintServices method. This selection is suitable for me.)

here is my question - how to get my full page content in java bean when I click the print button. A.

and another problem here,

when I do simple line printing with the following code, it does not detect errors in the code at compile time or runtime, but an error is displayed when printing to the printer (below). I get this code from online research. I am using a network printer.

enter image description here

print 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 as a spy process, but I need to print a page from a java bean.

update 1:

  • I can get the contents of the document.documentElement.outerHTML page in js (in fact, I don't want to use js to pass html content.this might be the last option if I can't find anything), and then pass it a bean from inputhidden. but now how to use this String content to print. A.
+2
source share

Source: https://habr.com/ru/post/1391802/


All Articles