我正在使用 Java SE 开发POS系统。我需要像超市账单一样打印长账单。
我希望使用 EPSON TM-U220D (https://www.epson.eu/products/sd/pos-printer/epson-tm-u220b-series)作为收据打印机。
但是问题是收据打印一段时间后会停止。。我使用了 java.awt.print.PrinterJob 来生成收据和打印。我从互联网上找到了代码。但是它没有按预期打印收据。
这是我在程序中使用的代码:
BillPrinter {
private DBQuery dbq;
private String billid;
private Bill bill;
private BillItem billitems[];
private int maxTextLength = 34;
private int y = 0;
private int yShift = 10;
public BillPrinter(DBQuery dbq) {
this.dbq = dbq;
}
public void printBill(String billid) {
this.billid = billid;
try {
bill = dbq.getBillByBillID(billid);
if (bill != null) {
billitems = dbq.getBillItemByBill(bill);
System.out.println(bill.getBillid() + " - " + bill.getId());
if (billitems.length > 0) {
PrinterJob printerJob = PrinterJob.getPrinterJob();
PageFormat pageFormat = printerJob.defaultPage();
Paper paper = new Paper();
paper = new Paper();
double **middleHeight** = billitems.length * 0.1;
double **headerHeight** = 0.5;
double **footerHeight** = 0.5;
double **width** = convert_CM_To_PPI(7);
double **height** = convert_CM_To_PPI(headerHeight + middleHeight + footerHeight);
paper.setSize(width, height);
pageFormat.setPaper(paper);
pageFormat.setOrientation(PageFormat.PORTRAIT);
paper.setImageableArea(
convert_CM_To_PPI(0.025),
convert_CM_To_PPI(0.05),
width - convert_CM_To_PPI(0.035),
height - convert_CM_To_PPI(0.1)
);
printerJob.setPrintable(new BillPrintable(), pageFormat);
try {
printerJob.print();
} catch (PrinterException ex) {
ex.printStackTrace();
}
}
}
} catch (NullPointerException npe) {
npe.printStackTrace();
}
}
private double convert_CM_To_PPI(double cm) {
return toPPI(cm * 0.393600787);
}
private double toPPI(double inch) {
return inch * 72d;
}
public class BillPrintable implements Printable {
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
throws PrinterException {
int result = NO_SUCH_PAGE;
if (pageIndex == 0) {
y = 10;
Graphics2D g2d = (Graphics2D) graphics;
g2d.translate((double) pageFormat.getImageableX(), (double)pageFormat.getImageableY());
try {
g2d.setFont(new Font("Monospaced", Font.BOLD, 10));
TextLengthFormater(g2d, "----------------------------------");
g2d.setFont(new Font("Monospaced", Font.BOLD, 10));
TextLengthFormater(g2d, "<Company Name>");
g2d.setFont(new Font("Monospaced", Font.PLAIN, 10));
TextLengthFormater(g2d, "<Address>");
TextLengthFormater(g2d, "<Contact>");
TextLengthFormater(g2d, "<Email>");
TextLengthFormater(g2d, "<Website>");
g2d.setFont(new Font("Monospaced", Font.BOLD, 10));
TextLengthFormater(g2d, "----------------------------------");
g2d.setFont(new Font("Monospaced", Font.PLAIN, 8));
TextLengthFormater(g2d, "Bill ID: " + billid);
TextLengthFormater(g2d, "Date: " + bill.getDate());
TextLengthFormater(g2d, "Description: " + bill.getDescription());
g2d.setFont(new Font("Monospaced", Font.BOLD, 10));
TextLengthFormater(g2d, "----------------------------------");
g2d.setFont(new Font("Monospaced", Font.PLAIN, 8));
for (int i = 0; i < billitems.length; i++) {
TextLengthFormater(g2d, billitems[i].getItem().getName());
StringBuilder sb = new StringBuilder("");
sb = addStringTo(sb, Integer.toString(billitems[i].getQuntity()), 3);
sb = addStringTo(sb, "X", 5);
sb = addStringTo(sb, Double.toString(billitems[i].getItem().getPrice()), 10);
sb = addStringTo(sb, "=", 5);
double amount = (billitems[i].getItem().getPrice() * billitems[i].getQuntity());
sb = addStringTo(sb, Double.toString(amount), 10);
TextLengthFormater(g2d, sb.toString());
}
g2d.setFont(new Font("Monospaced", Font.BOLD, 10));
TextLengthFormater(g2d, "----------------------------------");
g2d.setFont(new Font("Monospaced", Font.PLAIN, 8));
double total = bill.getTotal() * 100 /(100 - billitems[0].getDiscount());
TextLengthFormater(g2d, "Total: " + addStringTo(new StringBuilder(), Double.toString(total), 26).toString());
TextLengthFormater(g2d, "Discount: " + addStringTo(new StringBuilder(), Double.toString(total - bill.getTotal()), 23).toString());
TextLengthFormater(g2d, "Sub Total: " + addStringTo(new StringBuilder(), Double.toString((bill.getTotal())), 22).toString());
TextLengthFormater(g2d, "Pay: " + addStringTo(new StringBuilder(), Double.toString(bill.getPayment()), 28).toString());
TextLengthFormater(g2d, "Change: " + addStringTo(new StringBuilder(), Double.toString(bill.getPayment() - (bill.getTotal())), 25).toString());
g2d.setFont(new Font("Monospaced", Font.BOLD, 10));
TextLengthFormater(g2d, "----------------------------------");
TextLengthFormater(g2d, " THANK YOU");
TextLengthFormater(g2d, "----------------------------------");
} catch (Exception r) {
r.printStackTrace();
}
result = PAGE_EXISTS;
}
return result;
}
}
private void TextLengthFormater(Graphics2D g2d, String text) {
Vector<String> vtr = new Vector<String>();
while (text.length() > 0) {
vtr.add(text.substring(0, (text.length() > maxTextLength ? maxTextLength : text.length())));
text = text.substring((text.length() > maxTextLength ? maxTextLength : text.length()));
}
for (String str : vtr) {
g2d.drawString(str, 0, y);
y += yShift;
}
}
private StringBuilder addStringTo(StringBuilder sb, String value, int place) {
int count = 0;
while (value.length() + count < place) {
sb.append(" ");
count++;
}
sb.append(value);
return sb;
}}
问题出在哪里,我该如何解决?
我认为,我没有在 middleHeight 上设置正确的参数, headerHeight , footerHeight ,宽度和高度。
我还认为打印机的问题在于打印机只能以该长度打印。
或者我的代码中有问题要停止打印?还有另一种打印长期收据的方法吗?
我该如何解决?
您的帮助将不胜感激!