如何将TextArea打印到USB热敏打印机58mm?(JAVAFX)
所以我正在尝试制作一个账单系统,在其中我想打印一张收据。我可以用我在网上找到的一些代码来完成,但是字体太大,无法在58毫米宽的纸上打印。我无法调整字体大小。对此问题的任何帮助都将不胜感激。谢谢 代码如下:如何将TextArea打印到USB热敏打印机58mm?(JAVAFX),javafx,printing,fxml,thermal-printer,pos,Javafx,Printing,Fxml,Thermal Printer,Pos,所以我正在尝试制作一个账单系统,在其中我想打印一张收据。我可以用我在网上找到的一些代码来完成,但是字体太大,无法在58毫米宽的纸上打印。我无法调整字体大小。对此问题的任何帮助都将不胜感激。谢谢 代码如下: public class PrinterService implements Printable { public List<String> getPrinters(){ DocFlavor flavor = DocFlavor.BYTE_ARRAY.A
public class PrinterService implements Printable {
public List<String> getPrinters(){
DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
PrintService printServices[] = PrintServiceLookup.lookupPrintServices(
flavor, pras);
List<String> printerList = new ArrayList<String>();
for(PrintService printerService: printServices){
printerList.add( printerService.getName());
}
return printerList;
}
@Override
public int print(Graphics g, PageFormat pf, int page)
throws PrinterException {
if (page > 0) { /* We have only one page, and 'page' is zero-based */
return NO_SUCH_PAGE;
}
/*
* User (0,0) is typically outside the imageable area, so we must
* translate by the X and Y values in the PageFormat to avoid clipping
*/
Graphics2D g2d = (Graphics2D) g;
g2d.translate(pf.getImageableX(), pf.getImageableY());
/* Now we perform our rendering */
g.setFont(new Font("Roman", 0, 8));
g.drawString("Hello world !", 0, 10);
return PAGE_EXISTS;
}
public void printString(String printerName, String text) {
// find the printService of name printerName
DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
PrintService printService[] = PrintServiceLookup.lookupPrintServices(
flavor, pras);
PrintService service = findPrintService(printerName, printService);
DocPrintJob job = service.createPrintJob();
try {
byte[] bytes;
// important for umlaut chars
bytes = text.getBytes("CP437");
Doc doc = new SimpleDoc(bytes, flavor, null);
job.print(doc, null);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void printBytes(String printerName, byte[] bytes) {
DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
PrintService printService[] = PrintServiceLookup.lookupPrintServices(
flavor, pras);
PrintService service = findPrintService(printerName, printService);
DocPrintJob job = service.createPrintJob();
try {
Doc doc = new SimpleDoc(bytes, flavor, null);
job.print(doc, null);
} catch (Exception e) {
e.printStackTrace();
}
}
private PrintService findPrintService(String printerName,
PrintService[] services) {
for (PrintService service : services) {
if (service.getName().equalsIgnoreCase(printerName)) {
return service;
}
}
return null;
}
}
通常,对于标签打印机,您可以使用自己的打印机控制语言(如PCL、Postscript、ZPL、Fingerprint等)而不是Java的2D图形API对其进行编程。您使用的打印机型号是否附带Java SDK?我不熟悉,但您是否尝试过JavaFX附带的打印机型号?我尝试过JavaFX打印API,但它不适用于TextAreas。打印机没有附带Java SDK。它使用ESC/POS命令,但即使经过大量研究,我也无法在代码中实现,但我认为这是可能的。也许类似的东西会有帮助?
@FXML
public void printit(ActionEvent actionEvent)
{
PrinterService printerService = new PrinterService();
System.out.println(printerService.getPrinters());
//print some stuff
printerService.printString("POS-58-Series", area.getText());
}