英文:
How can I print to an actual printer?
问题
我有一个用Go语言编写的消息服务器。现在我有一个需求,希望服务器能够将一些消息打印到纸上。
在Go语言中,你可以如何实现这个功能呢?我在这个问题上遇到了很大的困难,找不到相关的资料。
这个应用程序将在Windows机器上运行,并且需要能够以固定宽度字体打印UTF8编码的文本。所以不需要任何花哨的格式(粗体文本、颜色等)。
对于如何实现这个功能,我一头雾水...有人可以给我指点迷津吗?指导一下我应该朝哪个方向努力?
英文:
I have a messaging server written in Go. Now I have a requirement that some messages need to be printed out on paper by the server.
How can I implement this in Go? I'm having a real hard time finding anything on the subject.
The app will be running on Windows machines and needs to be able to print UTF8 encoded text in a fixed width font. So no fancy formatting (bold text, color etc) is needed.
I'm rather completely in the dark on how to go about this... Can someone shed some light on this for me and point me in the right direction?
答案1
得分: 7
使用@abalos和@alex的答案,我能够使它按照我需要的方式工作。以下是一个使用alex的库的示例:
import prt "github.com/alexbrainman/printer"
...
name, err := prt.Default() // 返回默认打印机的名称作为字符串
if err != nil {
log.Fatal(err)
}
p, err := prt.Open(name) // 打开指定的打印机并返回*Printer
if err != nil {
log.Fatal(err)
}
err = p.StartDocument("test", "text") // test: 文档名称, text: 文档类型
if err != nil {
log.Fatal(err)
}
err = p.StartPage() // 开始新页面
if err != nil {
log.Fatal(err)
}
n, err := p.Write([]byte("Hello, Printer!")) // 向打印机发送一些文本
if err != nil {
log.Fatal(err)
}
fmt.Println("写入打印机的字节数:", n)
err = p.PageEnd() // 页面结束
if err != nil {
log.Fatal(err)
}
err = p.DocumentEnd() // 文档结束
if err != nil {
log.Fatal(err)
}
err = p.Close() // 关闭资源
if err != nil {
log.Fatal(err)
}
有关Windows API的更多详细信息,请参阅这里。
<details>
<summary>英文:</summary>
Using the answers from @abalos and @alex I was able to get this to work the way I need it to. Answering this to supply a sample of how to use it - it's pretty straightforward using alex's library:
import prt "github.com/alexbrainman/printer"
...
name, err := prt.Default() // returns name of Default Printer as string
if err != nil {
log.fatal(err)
}
p, err := prt.Open(name) // Opens the named printer and returns a *Printer
if err != nil {
log.fatal(err)
}
err = p.StartDocument("test", "text") // test: doc name, text: doc type
if err != nil {
log.fatal(err)
}
err = p.StartPage() // begin a new page
if err != nil {
log.fatal(err)
}
n, err := p.Write([]byte("Hello, Printer!")) // Send some text to the printer
if err != nil {
log.fatal(err)
}
fmt.Println("Num of bytes written to printer:", n)
err = p.PageEnd() // end of page
if err != nil {
log.fatal(err)
}
err = p.DocumentEnd() // end of document
if err != nil {
log.fatal(err)
}
err = p.Close() // close the resource
if err != nil {
log.fatal(err)
}
More details on the Windows API can be found [here][1]
[1]: https://msdn.microsoft.com/en-us/library/windows/desktop/dd162863(v=vs.85).aspx
</details>
# 答案2
**得分**: 3
可以使用Go语言调用正确的命令行参数来打印所需的文件。你只需要先将这些信息打印到一个文件中。
请参考[Microsoft TechNet][1]上的相关信息。
另一种我不太熟悉的方法是使用Go语言通过调用Windows中的DLL来实现打印功能。我没有找到太多关于这方面的信息,但是这个[Go文档提供了一些很好的示例][2]。
你可以尝试这几个方向! :)
[1]: https://technet.microsoft.com/en-us/library/cc775908%28v=ws.10%29.aspx
[2]: https://code.google.com/p/go-wiki/wiki/WindowsDLLs
<details>
<summary>英文:</summary>
It's possible to use Go to call the correct command line arguments to print whatever files are needed. You'd just need to print this information to a file first.
Please see the information on [Microsoft TechNet][1] for this method.
Another method I am less familiar with is to use the DLL's present in Windows through Go to invoke printing. I wasn't able to find as much information on this, but this [Go documentation has some pretty good examples.][2]
There are a couple directions you can look in! :)
[1]: https://technet.microsoft.com/en-us/library/cc775908%28v=ws.10%29.aspx
[2]: https://code.google.com/p/go-wiki/wiki/WindowsDLLs
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论