Search code examples
goprintingthermal-printerescpos

How can I print in Go?


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?


Solution

  • 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
    }