我在Java中使用PrinterService和PrinterJob在POS58系列打印机中进行打印;有时它开始将标题垃圾字符打印为以下图像:
以下代码:
public class BillPrintableIngreso implements Printable {
@Override
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
int result = NO_SUCH_PAGE;
if (pageIndex == 0) {
Graphics2D g2d = (Graphics2D) graphics;
double width = pageFormat.getImageableWidth();
g2d.translate((int) pageFormat.getImageableX(),(int) pageFormat.getImageableY());
FontMetrics metrics=g2d.getFontMetrics(new Font("Arial",Font.BOLD,7));
int idLength=metrics.stringWidth("000");
int amtLength=metrics.stringWidth("000000");
int qtyLength=metrics.stringWidth("00000");
int priceLength=metrics.stringWidth("000000");
int prodLength=(int)width - idLength - amtLength - qtyLength - priceLength-17;
int productPosition = 0;
int discountPosition= prodLength+5;
int pricePosition = discountPosition +idLength+10;
int qtyPosition=pricePosition + priceLength + 4;
int amtPosition=qtyPosition + qtyLength;
/*Draw Header*/
int y=20;
int yShift = 10;
int headerRectHeight=15;
int headerRectHeighta=40;
g2d.setFont(new Font("Monospaced",Font.BOLD,9));
g2d.drawString(" Conjunto Residencial ",0,y);y+=yShift;
g2d.drawString(" Ciudad Tintal Casas ",0,y);y+=yShift;
g2d.drawString(" Calle 8a # 92-71 ",0,y);y+=yShift;
g2d.drawString(" Bogotá Colombia ",0,y);y+=yShift;
g2d.drawString("************************",0,y);y+=headerRectHeight;
g2d.setFont(new Font("Monospaced",Font.BOLD,8));
g2d.drawString("# Ticket: "+nticket,0,y);y+=yShift;
g2d.drawString("Placa: "+placa,0,y);y+=yShift;
g2d.drawString("Nombre: "+nombreConductor,0,y);y+=yShift;
g2d.drawString("Casa visitada: "+casaVisitada,0,y);y+=yShift;
g2d.drawString("Ingreso: "+fechaIngreso,0,y);y+=yShift;
g2d.drawString("\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r",0,y);y+=headerRectHeighta;
g2d.drawString("- - - - - - - - - - - - - -",0,y);y+=headerRectHeighta;
result = PAGE_EXISTS;
}
return result;
}
}
PrinterService printerService = new PrinterService();
List <String> printers=printerService.getPrinters();
if (printers.size()>0){
String printer=printers.get(0).toString();
loguer("Imprimiendo en: "+printers.get(0).toString());
loguer(printer);
PrinterJob pj = PrinterJob.getPrinterJob();
pj.setPrintable(new BillPrintableIngreso(),getPageFormat(pj));
try {
pj.print();
} catch (PrinterException ex) {
loguer(ex.getLocalizedMessage());
}
}else {
Dialogs.dialogError("Error", "No hay impresora conectada...", "");
}
请帮我解决这个问题!
我在Ubuntu 18上使用JAVAFX
最好的问候
Nelson Rodriguez