【发布时间】:2017-07-04 01:24:17
【问题描述】:
我从 Web 服务收到一个字符串,我想在 ESC/POS 打印机上打印它。我试过这个:
private void print()
{
PrintService ps = getPrinter(deviceSystemName);
byte[] commandByteArray = decodeReceiptCommandString();
DocFlavor df = DocFlavor.INPUT_STREAM.AUTOSENSE;
ByteArrayInputStream pis = new ByteArrayInputStream(commandByteArray);
Doc d = new SimpleDoc(pis, df, null);
if (ps != null) {
DocPrintJob job = ps.createPrintJob();
job.print(d, null);
}
}
private byte[] decodeReceiptCommandString()
{
String encoding = "Cp850";
String commandString = new String(this.receipt.getString("stringa"));
return commandString.getBytes(encoding);
}
这在意大利系统中运行良好,但是当我在客户的西班牙打印机上打印时,结果就不一样了。
我的工作打印:
在我客户的打印机上打印:
我的打印机的协议和字符集与我的客户打印机相同。
出了什么问题,我该如何解决?
【问题讨论】:
-
什么是
this.receipt.getString("stringa"),它是如何初始化的,它来自哪里?如果它已经是一个字符串,那么在它上面使用new String()是没有意义的。如果它来自某个配置文件,请确保使用正确的编码读取文件。 -
this.receipt 是 JSONObject,我必须用 .getString(key) 获取内容,key 是收据 JSONObject 收据结构 = { id : int , stringa:String };
-
那么你有没有在打印前检查过这个字符串没有被破坏?我的猜测是问题可能出在其他地方......
-
您是否确保将打印机设置为与您发送给它的数据相同的代码页?要设置代码页 850,请将 ESC t 0x02 发送到打印机。
-
您使用的是什么打印机(品牌、型号)?
标签: java character-encoding epson escpos