繁体   English   中英

如何在 Go 中打印?

[英]How can I print in Go?

我正在使用 package 使用 ESC-POS 命令在热敏打印机上打印,我已经生成了要打印的文件,但是要打印它,我使用 CMD 命令: print /D:\\myPC\\POS-58-Series printfile

我尝试使用: exec.Command("print", "/D:\\myPC\\POS-58-Series", "printfile").Run()但它没有用

有没有办法从 Go 使用我正在尝试执行的命令进行打印,或者我是否需要使用其他东西?

取决于您是否需要格式化 output... 但您可能需要查看https://github.com/alexbrainman/printer以查看这是否解决了您的打印问题。

终于搞定了,稍微找了一下找到了mike42的escpos-php库。

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我找到了 php2golang 页面,在那里我找到了一种方法。

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

所以,我的最终代码是

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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM