【发布时间】:2012-04-10 12:00:34
【问题描述】:
我打算使用 Eclipse 和一个简单的 swing gui 来打印原始数据。我的条码打印机使用.prn 文件进行打印,所有文件都被编码在文件中,所以如果我能设法将文件的确切内容作为原始数据发送到我的打印机,我可以让它打印出我想要的确切格式。我可以选择文件并使用StringBuffer 读取其内容并写入字符串。现在我怎样才能设法将此字符串作为原始数据发送到我的打印机?
问候。
编辑:
也许我应该详细说明一下我的问题;现在它可以工作了,在 Windows 中使用;
int ch;
FileInputStream fin = null;
try {
fin = new FileInputStream(prnfile);
while ((ch = fin.read()) != -1)
strContent.append((char) ch);
fin.close();
JOptionPane.showMessageDialog(frame, strContent);
} catch (Exception e1) {
System.out.println(e1);
}
try {
PrintService service = PrintServiceLookup.lookupDefaultPrintService();
JOptionPane.showMessageDialog(frame, service);
DocPrintJob job = service.createPrintJob();
InputStream is = new ByteArrayInputStream(strContent.toString().getBytes());
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
Doc doc = new SimpleDoc(is, flavor, null);
job.print(doc, null);
is.close();
但是当我尝试在 Ubuntu 中运行它时,我只收到“打印取消,java 打印”通知,而打印机什么也不做(通常我的打印机在 ubuntu 中使用终端上的cat xxx.prn | lpr 工作。有什么想法吗?
【问题讨论】:
-
您可能不应该使用
String,并且如何将其发送到您的打印机取决于打印机及其提供的接口类型。那么:它是一种什么样的打印机,它提供了哪些接口?到目前为止,您尝试过什么? -
我设法让它实际工作,但仅在 Windows 中,但当我在 Ubuntu 中运行它时,它只是说“打印取消”“java 打印”......
-
打印机通常需要通过 CUPS 或 GUI 进行原始打印配置。 stackoverflow.com/a/38965500/3196753。一些 Windows 驱动程序会自动感知可能描述差异的数据类型(Epson 和 Zebra 会这样做)。
标签: java eclipse ubuntu printing ubuntu-11.10