duandie5707 2015-04-07 12:04
浏览 144
已采纳

如何打印到实际打印机?

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?

  • 写回答

2条回答 默认 最新

  • duancanjiu3754 2015-04-08 10:52
    关注

    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条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘