anastaciocintra / escpos-coffee

Java library for ESC/POS printer
https://anastaciocintra.github.io/escpos-coffee
MIT License
282 stars 74 forks source link

How to print simplified chinese #81

Open BanLam opened 2 years ago

BanLam commented 2 years ago

Desktop (Info): OS: Windows 10 Lib Version 4.1.0 Java Version 1.8.0_181 Pinter: EPSON TM-m30II

code: escpos.setCharsetName("GB2312"); escpos.writeLF("文档");

it's not working, i try all the encoding with simplified chinese already. https://docs.oracle.com/javase/8/docs/technotes/guides/intl/encoding.doc.html

Thank you. Regards.

anastaciocintra commented 2 years ago

hi @BanLam, its a good question about character code table, I've saw this article https://stackoverflow.com/questions/63546719/esc-pos-termal-printer-utf-8-charset-set-up and this doc https://reference.epson-biz.com/modules/ref_escpos/index.php?content_id=337

And for your printer, (in theory) is possible to print utf8 characters. I can't test in mine, but you can test on tm-m30ii, if it is possible, send us feedback about test. see you.

import com.github.anastaciocintra.escpos.EscPos;
import com.github.anastaciocintra.output.PrinterOutputStream;

import javax.print.PrintService;
import java.io.IOException;

public class Fs {

    public void sendData(EscPos escpos) throws IOException {
        setUtf8((escpos));
        escpos.writeLF("文档");
        escpos.writeLF("文档");
        escpos.writeLF("文档");
        escpos.writeLF("文档");
        escpos.writeLF("文档");
        escpos.writeLF("文档");
        escpos.writeLF("文档");
        escpos.writeLF("文档");
        escpos.writeLF("文档");
        escpos.writeLF("文档");
        escpos.feed(5).cut(EscPos.CutMode.FULL);
        escpos.close();
    }
    private void setUtf8(EscPos escpos) throws IOException{
        final int m = 2;
        //
        //FS ( C pL pH fn m
        escpos.write(28) //FS
                .write('(') // (
                .write('C')  // C
                .write(2) // pl
                .write(0) // ph
                .write(48) // fn
                .write(m); // m
        escpos.setCharsetName("UTF8");
    }

    public static void main(String[] args) throws IOException {

        if(args.length!=1){
            System.out.println("Usage: java -jar escpos-simple.jar (\"printer name\")");
            System.out.println("Printer list to use:");
            String[] printServicesNames = PrinterOutputStream.getListPrintServicesNames();
            for(String printServiceName: printServicesNames){
                System.out.println(printServiceName);
            }

            System.exit(0);
        }
        //this call is slow, try to use it only once and reuse the PrintService variable.
        PrintService printService = PrinterOutputStream.getPrintServiceByName(args[0]);
        PrinterOutputStream printerOutputStream = new PrinterOutputStream(printService);
        EscPos escpos = new EscPos(printerOutputStream);
        Fs obj = new Fs();
        obj.sendData(escpos);
    }

}