我一直在尝试使用热敏打印机“Bixolon SRP-F310”并使用 JAVA 的 PrintService 打印一些文本。检测到打印机,调用打印函数时没有异常。我可以在 Cups 的 Web 界面中看到调用了打印事件。但是打印机不打印并且错误消息“未找到页面!” 可以在 Cups 的网页界面中看到。任何帮助将不胜感激。我已经包含了 Cups 网络界面的屏幕截图和错误日志。
import javax.print.*;
import java.util.Arrays;
import java.util.List;
public class Printer {
static Printer INSTANCE;
public static void main(String[] args) {
INSTANCE = new Printer();
List<PrintService> services = INSTANCE.getServicesByName("BIXOLON_SRP-F310");
if(services == null) {
throw new RuntimeException("No printer services available");
}
INSTANCE.printServices(services);
try {
INSTANCE.print(services.get(0), "Hello");
} catch (Exception e) {
e.printStackTrace();
}
}
public List<PrintService> getServicesByName(String serviceName) {
//Find printer service by name
AttributeSet aset = new HashAttributeSet();
aset.add(new PrinterName(serviceName, null));
return Arrays.asList(PrintServiceLookup.lookupPrintServices(null, aset));
}
public void print(PrintService service, String printData) throws Exception {
if(service == null) {
throw new Exception("Service is not valid");
}
if(printData == null) {
throw new Exception("Nothing to print");
}
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
pras.add(new Copies(1));
pras.add(new PrinterResolution(180,180,PrinterResolution.DPI));
DocPrintJob job = service.createPrintJob();
DocAttributeSet das = new HashDocAttributeSet();
das.add(new PrinterResolution(180,180,PrinterResolution.DPI));
byte[] desc = printData.getBytes();
Doc doc = new SimpleDoc(desc, DocFlavor.BYTE_ARRAY.AUTOSENSE, das);
try {
job.print(doc, pras);
} catch (Exception e) {
e.printStackTrace();
}
}
public void printServices(List<PrintService> services) {
System.out.println("Printer Services found:");
for (PrintService service : services) {
System.out.println("\t" + service);
}
}
}
杯子的网页界面:

错误日志: