I have print method for html document in my application. I have no access to a physical printer. since I didn’t connect the computer to a physical printer I got the “no printer service found” as specified in code. can I check that function work properly when I connect the computer to a printer without physical printer?
FileInputStream psStream = null;
try {
psStream = new FileInputStream("c:\\some.html");
} catch (FileNotFoundException ffne) {
ffne.printStackTrace();
}
if (psStream == null) {
return;
}
DocFlavor htmlStreamFlavor = new DocFlavor("text/html; charset=utf-16", "java.io.InputStream");
Doc myDoc = new SimpleDoc(psStream, htmlStreamFlavor, null);
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
PrintService[] services = PrintServiceLookup.lookupPrintServices(htmlStreamFlavor, aset);
// if several printers configured
PrintService myPrinter = null;
for (int i = 0; i < services.length; i++){
String svcName = services[i].toString();
System.out.println("service found: "+svcName);
if (svcName.contains("printer closest to me")){
myPrinter = services[i];
System.out.println("my printer found: "+svcName);
break;
}
}
if (myPrinter != null) {
DocPrintJob job = (DocPrintJob) myPrinter.createPrintJob();
try {
job.print(myDoc, aset);
} catch (Exception pe) {pe.printStackTrace();}
} else {
System.out.println("no printer services found");
}
There are logical printers that create a pdf instead of printing. For your application they look just like a normal printer.
See this rather long list for options: https://www.google.de/search?q=pdf+printer