0

I am working on a package to print on thermal printers with ESC-POS commands, I already generate the file to print, but to print it I use the CMD command: print /D:\\myPC\\POS-58-Series printfile

I tried to use: exec.Command("print", "/D:\\myPC\\POS-58-Series", "printfile").Run() but it didn't work

Is there a way to print from Go with commands like I am trying to do or do I need to use something else?

Mofi
  • 46,139
  • 17
  • 80
  • 143
  • If you search, you will find something like this, so why not give it a try? [kenshaw/escpos](https://github.com/kenshaw/escpos), [Source file src/go/printer/printer.go](https://golang.org/src/go/printer/printer.go), [I'm using go language to print to a POS printer, but got bad prints of Chinese charater. (English is OK)](https://stackoverflow.com/q/51434977/9014308) – kunif Apr 01 '21 at 08:21

1 Answers1

1

Finally I did it, looking a bit I found the escpos-php library of mike42.

https://github.com/mike42/escpos-php

The problem is that it uses php and I needed it in go, reviewing its code I discovered that it uses the copy function to send the file with the esc/pos code to the printer, unfortunately in go it is not that easy, but fortunately I found the page php2golang where I found a way to do it.

https://www.php2golang.com/method/function.copy.html

So, my final code is

func main () {
    Copy ("printfile", "\\\\myPC\\\\POS-58-Series")
}

func Copy (source, dest string) (bool, error) {
    fd1, err: = os.Open (source)
    if err! = nil {
        return false, err
    }

    defer fd1.Close ()
    fd2, err: = os.OpenFile (dest, os.O_WRONLY | os.O_CREATE, 0644)
    if err! = nil {
        return false, err
    }

    defer fd2.Close ()
    _, e: = io.Copy (fd2, fd1)
    if e! = nil {
        return false, e
     }
    return true, nil
}