JavaFx 如何打印文本(仅)收据?

Gad*_*ado 1 java printing javafx

我正在构建一个销售点应用程序,我想打印收据。问题是,使用我使用的打印机,我无法仅打印纯文本图形,而我在 javafx 中所能找到的只是使用 Print API 打印节点或使用像 jasper 这样的报告工具,它们都包含图形。

我想制作的收据看起来像这张 收据

感谢您的时间

mr *_*olf 5

这是来自ESC/POS打印机的笔记。尽管可以使用 OS 打印服务在此类打印机上打印,但最好与其直接通信。

基本上,打印文本足以将其发送到打印机 + \n( 0x0A)。这些打印机中有 2 种字体可以设置为有限的样式(双高、双宽、粗体、斜体、下划线...)。它们还支持不同类型的条形码(如果需要,它们自己计算校验和,并自己绘制条形码)。

它们的界面通常是RS232USB(虚拟RS - V irtual小号erial P ORT)。

您可以使用javax.comm实现在 java(fx) 应用程序上的此类打印机上打印。我个人使用RXTX。打印机的通信协议通常是兼容的(至少在文本打印的主流中),但不能保证。所以很高兴与你知道的模型一起工作。

这是在我使用的打印机上打印类似于您的笔记的示例。应用程序通常是一个java应用程序,但使用这种javafx打印方式是没有问题的。

package posprintdemo;

import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import java.io.OutputStream;

public class POSPrintDemo {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception {
        String portName = "/dev/ttyS4";
        Integer baudrate = 57600;
        Integer timeout = 1000;

        SerialPort serialPort = (SerialPort)CommPortIdentifier.getPortIdentifier(portName).open(POSPrintDemo.class.getName(), 1000);
        serialPort.setSerialPortParams(baudrate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
        serialPort.enableReceiveTimeout(timeout);

        try(OutputStream os = serialPort.getOutputStream()) {
            // select double width and height font
            os.write(new byte[] {0x1b, 0x21, 0x31});

            os.write("       AROMA CAFE\n".getBytes());
            os.write("   1211 Green Street\n".getBytes());
            os.write("      New York, NY\n".getBytes());

            // select normal font
            os.write(new byte[] {0x1b, 0x21, 0x01});

            os.write("03-12-2016       1:11PM\n".getBytes());
            os.write("TBL 1            HOST ALISON\n".getBytes());
            os.write("VISA ######8281\n".getBytes());
            os.write("\n".getBytes());
            os.write("QTY  DESC                              AMT\n".getBytes());
            os.write("----------------------------------------------\n".getBytes());
            os.write("1   GINGER CARROT SOUP                   $6.79\n".getBytes());
            os.write("1   HOUSE SALAD                          $7.69\n".getBytes());
            os.write("1   SURF AND RUTF - 1 PERS              $48.79\n".getBytes());
            os.write("1   WINE - GLASS - FIXE                 $11.50\n".getBytes());
            os.write("1   CHOC CAKE                            $6.75\n".getBytes());
            os.write("\n".getBytes());

            // select double width and height font
            os.write(new byte[] {0x1b, 0x21, 0x31});
            os.write("    AMOUNT    $90.52\n".getBytes());

            os.write(new byte[] {0x1b, 0x21, 0x01});
            os.write("\n".getBytes());
            os.write("        SUB-TOTAL           $81.52\n".getBytes());
            os.write("        TAX                  $9.00\n".getBytes());
            os.write("        BALANCE             $90.52\n".getBytes());
            os.write("\n".getBytes());
            os.write("\n".getBytes());
            os.write("\n".getBytes());

            // center text
            os.write(new byte[] {0x1b, 0x61, 0x31}); 

            // set barcode height to 80px
            os.write(new byte[] {0x1d, 0x68, 0x50}); 

            // print CODE39 with text TEST
            os.write(new byte[] {0x1d, 0x6b, 0x45, 0x04, 'T', 'E', 'S', 'T'});
            os.flush();
        }
    }   
}
Run Code Online (Sandbox Code Playgroud)

这是收到的便条(印在 57 毫米宽的纸上)

这是收到的便条