2

I'm using this go package, and everything is good. But got bad prints of Chinese characters. I'm not good at this kind of low-level / hardware / encoding things. So someone give a hint how can I correct my code.

Updated code:

func main() {
    testWindowPrinter()
}

func testWindowPrinter() {
    // all have bad prints
    printLines(simplifiedchinese.GBK.NewDecoder().Reader(strings.NewReader("你好")))
    printLines(simplifiedchinese.HZGB2312.NewDecoder().Reader(strings.NewReader("你好")))
    printLines(strings.NewReader("你好"))
}

func printLines(lines ...io.Reader) error {
    var defaultPrinter, err = printer.Default()
    if err != nil {
        log.Println(1, err.Error())
        return err
    }

    p, err := printer.Open(defaultPrinter)
    if err != nil {
        log.Println(2, err.Error())
        return err
    }
    defer p.Close()

    err = p.StartRawDocument("test")
    if err != nil {
        log.Println(3, err.Error())
        return err
    }
    defer p.EndDocument()

    err = p.StartPage()
    if err != nil {
        log.Println(4, err.Error())
        return err
    }

    // Am I doing right here?
    for i := range lines {
        var bf = new(bytes.Buffer)
        n, err := bf.ReadFrom(lines[i])
        if err != nil {
            println(err.Error())
        } else {
            println(n)
            fmt.Fprintln(p, bf.String())
        }
    }

    err = p.EndPage()
    if err != nil {
        log.Println(5, err.Error())
        return err
    }

    return nil
}

More details zsbd

Leeds
  • 35
  • 1
  • 10

1 Answers1

1

It depends on your environment.

If you are executing this Go program in a Windows CMD (which only supports UTF16 encoding), you will see strange characters.
See "How to properly output a string in a Windows console with go?" as an example.
You need to convert those characters before printing them out.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks @VonC, yeah I executed the code in Windows CMD, I'll check the link. Thank you so much. – Leeds Jul 20 '18 at 04:47
  • Hi @VonC, I still get bad prints using `charmap.ISO8859_1`. I lookup my CMD settings and it says code page CP936, but I can't find it in `golang.org/x/text/encoding/charmap`. So any suggestions? – Leeds Jul 20 '18 at 05:44
  • @Leeds It now is in https://github.com/golang/text/blob/master/encoding/charmap/charmap.go – VonC Jul 20 '18 at 05:54
  • oops Chinese related is located in `text/encoding/simplifiedchinese`. But I still get the bad prints. this issue makes me crazy! – Leeds Jul 20 '18 at 06:28
  • @Leeds Did yu change the font of your CMD console? (https://stackoverflow.com/a/22828826/6309): Lucida or Consola. – VonC Jul 20 '18 at 07:09
  • Hi @VonC, nope and my CMD settings does not has Font like Lucida and Consola. I'm guessing the package (https://github.com/alexbrainman/printer) does not support Chinese? – Leeds Jul 20 '18 at 07:29
  • @Leeds It does, but your console must use a compatible font: https://www.isunshare.com/windows-8/change-font-font-size-and-layout-in-cmd.html and https://www.howtogeek.com/howto/windows-vista/stupid-geek-tricks-enable-more-fonts-for-the-windows-command-prompt/ – VonC Jul 20 '18 at 07:30
  • Just making sure I describe my issue right, I'm printing to POS-printer, not CMD console. Is my issue related to CMD conosle? – Leeds Jul 20 '18 at 07:38
  • @Leeds I missed the "POS printer" part (I am not familiar with this): CMD is just a way to validate your output. – VonC Jul 20 '18 at 07:48
  • Hi @VonC, I updated my code, would like double review my code? – Leeds Jul 20 '18 at 07:49
  • Looks good, but the issue might be on the printer side. For instance: https://github.com/mike42/escpos-php/issues/254 – VonC Jul 20 '18 at 07:52
  • Also https://stackoverflow.com/a/48421498/6309. Both links are not for Go but can give an idea of what is needed. Nice illustration: https://stackoverflow.com/q/48531109/6309 – VonC Jul 20 '18 at 07:54
  • I'm sure the POS-printer is OK, because it works fine if I use a commercial cashier software. Only got bad prints when I run my code. I'll check the links you suggested and ping you back here later. Thanks @VonC – Leeds Jul 20 '18 at 08:02