How can I print to an actual printer?

IamNaN

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?

IamNaN

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I print JasperPrint directly to printer?

From Dev

How can I print JasperPrint directly to printer?

From Dev

How can I use JavaPOS to print reciepts with an Epson printer?

From Dev

how can I print from a different subnet than the printer?

From Dev

How can I copy a print file to a networked printer?

From Dev

How can I easily set up a Google Cloud Print printer?

From Dev

How can I print PHP syntax highlighting on a colour printer?

From Dev

How many pages can I continuously print without damaging the printer?

From Dev

nodejs loglevel: how can I print out the actual level of the log?

From Dev

How should i remove printer image on print?

From Dev

How can I force my printer to print in black when it can't recognize 2 colour cartridges?

From Dev

How can I force a color printer to print in black and white, when only the color cartridges are allegedly empty?

From Dev

How can I install my printer by terminal?

From Dev

How can I install my printer by terminal?

From Dev

Can a printer print white color?

From Dev

LPR print, can it be directly to printer?

From Dev

how do I convert this code to NOT ask for a printer, just print to default printer?

From Dev

How can I print this?

From Dev

How to print image in TSC printer

From Dev

How to print no of copies from printer

From Dev

How to print to directly to a wireless printer

From Dev

How to remove Print to File printer?

From Dev

How to print to directly to a wireless printer

From Dev

How can I set up a usb printer as a network printer using a linux server?

From Dev

How can I deselect a default printer in Windows 7 and get back to no printer selected as default?

From Dev

How can I install a printer driver from a (local) ppd file?

From Dev

How can I calibrate my printer using the LaTeX test page?

From Dev

How can i ping a Computer or a Printer using its Mac address?

From Dev

How can I simulate a USB printer to LPT on Linux?

Related Related

  1. 1

    How can I print JasperPrint directly to printer?

  2. 2

    How can I print JasperPrint directly to printer?

  3. 3

    How can I use JavaPOS to print reciepts with an Epson printer?

  4. 4

    how can I print from a different subnet than the printer?

  5. 5

    How can I copy a print file to a networked printer?

  6. 6

    How can I easily set up a Google Cloud Print printer?

  7. 7

    How can I print PHP syntax highlighting on a colour printer?

  8. 8

    How many pages can I continuously print without damaging the printer?

  9. 9

    nodejs loglevel: how can I print out the actual level of the log?

  10. 10

    How should i remove printer image on print?

  11. 11

    How can I force my printer to print in black when it can't recognize 2 colour cartridges?

  12. 12

    How can I force a color printer to print in black and white, when only the color cartridges are allegedly empty?

  13. 13

    How can I install my printer by terminal?

  14. 14

    How can I install my printer by terminal?

  15. 15

    Can a printer print white color?

  16. 16

    LPR print, can it be directly to printer?

  17. 17

    how do I convert this code to NOT ask for a printer, just print to default printer?

  18. 18

    How can I print this?

  19. 19

    How to print image in TSC printer

  20. 20

    How to print no of copies from printer

  21. 21

    How to print to directly to a wireless printer

  22. 22

    How to remove Print to File printer?

  23. 23

    How to print to directly to a wireless printer

  24. 24

    How can I set up a usb printer as a network printer using a linux server?

  25. 25

    How can I deselect a default printer in Windows 7 and get back to no printer selected as default?

  26. 26

    How can I install a printer driver from a (local) ppd file?

  27. 27

    How can I calibrate my printer using the LaTeX test page?

  28. 28

    How can i ping a Computer or a Printer using its Mac address?

  29. 29

    How can I simulate a USB printer to LPT on Linux?

HotTag

Archive