作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在裁纸时遇到问题。我有 Epson TM-T20 收据,打印完成后尝试裁纸。我在某处发现这是用于切割 byte[] bytes = { 27, 100, 3 }
的代码,但它不起作用。
下面是我用于打印的代码。
public static void printer(String printerData, Boolean bill) throws IOException {
try {
PrintService[] printServices = PrinterJob.lookupPrintServices();
String testData = printerData + "\r";
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] bytes = { 27, 100, 3 }; //Code for cutting
outputStream.write(testData.getBytes(Charset.forName("UTF-8")));
outputStream.write(bytes);
InputStream is = new ByteArrayInputStream(outputStream.toByteArray());
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
PrintService service = PrintServiceLookup.lookupDefaultPrintService();
System.out.println(service);
DocPrintJob job = service.createPrintJob();
Doc doc = new SimpleDoc(is, flavor, null);
PrintJobWatcher pjDone = new PrintJobWatcher(job);
job.print(doc, null);
pjDone.waitForDone();
is.close();
} catch (PrintException e) {
Writer fw = new OutputStreamWriter(new FileOutputStream("log.txt", true), StandardCharsets.UTF_8);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter writer = new PrintWriter(bw);
e.printStackTrace(writer);
writer.close();
} catch (IOException e) {
Writer fw = new OutputStreamWriter(new FileOutputStream("log.txt", true), StandardCharsets.UTF_8);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter writer = new PrintWriter(bw);
e.printStackTrace(writer);
writer.close();
}
}
最佳答案
问题在于,只有字符串被发送到打印机,而不是关于打印状态的信息。因此,类和方法完全改变了。
public class Printing implements Printable {
private String stringToPrint;
public Printing(String stringToPrint) {
this.stringToPrint = stringToPrint;
}
@Override
public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException {
if (pageIndex >= 1) {
return Printable.NO_SUCH_PAGE;
}
g.setColor(Color.black);
g.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 10));
g.translate(0, 0);
int x = 0;
int y = 100;
//
for (String line : stringToPrint.split("\n")) {
g.drawString(line, x, y += g.getFontMetrics().getHeight());
}
return Printable.PAGE_EXISTS;
}
public static void printer(String printerData, String designatedPrinter)
throws IOException, PrinterException {
try {
PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
PrintService designatedService = null;
PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null);
AttributeSet aset = new HashAttributeSet();
aset = new HashAttributeSet();
aset.add(ColorSupported.NOT_SUPPORTED);
String printers = "";
for (int i = 0; i < printServices.length; i++) {
printers += " service found " + printServices[i].getName() + "\n";
}
for (int i = 0; i < printServices.length; i++) {
System.out.println(" service found " + printServices[i].getName());
if (printServices[i].getName().equalsIgnoreCase(designatedPrinter)) {
System.out.println("I want this one: " + printServices[i].getName());
designatedService = printServices[i];
break;
}
}
Writer fw = new OutputStreamWriter(new FileOutputStream("printing.txt"), StandardCharsets.UTF_8);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter writer = new PrintWriter(bw);
writer.print(printers);
writer.close();
PrinterJob pj = PrinterJob.getPrinterJob();
pj.setPrintService(designatedService);
Printable painter;
// Specify the painter
painter = new Printing(printerData);
pj.setPrintable(painter);
pj.print();
} catch (PrinterException e) {
Writer fw = new OutputStreamWriter(new FileOutputStream("log.txt", true), StandardCharsets.UTF_8);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter writer = new PrintWriter(bw);
e.printStackTrace(writer);
writer.close();
}
}
}
关于java - 爱普生打印完成后如何裁纸?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49016700/
我是一名优秀的程序员,十分优秀!