Created
October 14, 2017 21:58
-
-
Save ndrewnee/96fe5416e7138d322c9bb4dddf926817 to your computer and use it in GitHub Desktop.
Example of printing barcodes on USB printer "Zebra" https://www.zebra.com/ru/ru.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"flag" | |
"fmt" | |
"io/ioutil" | |
"os" | |
"os/exec" | |
) | |
var ( | |
host = flag.String("h", "", "name of host where printer is hosted") | |
printer = flag.String("p", "ZEBRA", "name of shared printer zebra") | |
) | |
const ( | |
zplCommand = `^XA | |
^FO 20,20 | |
^BY3 | |
^BCN,100,Y,N,N | |
^FD192837465^FS | |
^XZ` | |
) | |
func init() { | |
flag.Parse() | |
} | |
func main() { | |
tmpFile, err := ioutil.TempFile("", "zebra") | |
if err != nil { | |
panic(err) | |
} | |
defer func() { | |
removeErr := os.Remove(tmpFile.Name()) | |
if removeErr != nil { | |
fmt.Println("Remove temp file error: ", removeErr) | |
} | |
}() | |
_, err = tmpFile.Write([]byte(zplCommand)) | |
if err != nil { | |
panic(err) | |
} | |
err = tmpFile.Close() | |
if err != nil { | |
panic(err) | |
} | |
if host == nil || *host == "" { | |
hostName, err := os.Hostname() | |
if err != nil { | |
panic(err) | |
} | |
host = &hostName | |
} | |
sharedPrinterName := fmt.Sprintf("\\\\%s\\%s", *host, *printer) | |
cmd := exec.Command("cmd", "/C", "copy", tmpFile.Name(), sharedPrinterName) | |
err = cmd.Run() | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println("Printed!") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment