usertyfuture13
usertyfuture13

Reputation: 11

Print User input to physical printer in Golang

I'm new here in Golang world for a week.. I've been research and still don't know how to print from user input to physical printer, from USB to printer.

For example I have done with code, just need add other function to print..

package main

import "fmt"

func main() {

   fmt.Println("First name: ")

   var name1 string
   fmt.Scanln(&name1)
   )
   fmt.Println("Last name: ")

   var name2 string
   fmt.Scanln(&name2)

   fmt.Print("My full name is: ")
   fmt.Print(name1 + " " + name2)
}

func print(){

//need function to print directly from USB to pyhsical printer..
//print from func main() as text, any format

}

I have no idea to create print func in golang from user input..

If successful, then after func main() println, then immediately send the command to print to my printer.. just using terminal, I don't need the GUI.

Thank you for helping me:')

Upvotes: 1

Views: 600

Answers (1)

Jadefox10200
Jadefox10200

Reputation: 574

I'm only aware of sending documents to print.

You can check out the repo I made to see if this works for you.

Go Print

I haven't tried it before but in theory you could try the code below taken from this repo to send text directly to the printer rather than pulling the text from a file. I don't currently have a windows computer set up with a printer. If you have a mac or linux OS I would suggest taking a look at this repo and specifically the sendFile function where it shows how this is done. Again, in theory rather than opening a file and sending the bytes, you can simply pass in the bytes directly: https://github.com/documatrix/go-lprlib/blob/v0.4.3/lpr_send.go#L348

var dll = syscall.MustLoadDLL("winspool.drv")
var writePrinter = dll.MustFindProc("WritePrinter")
var getDefaultPrinter = dll.MustFindProc("GetDefaultPrinterW")
var openPrinter = dll.MustFindProc("OpenPrinterW")

func main() {

 text := "Text to be printed"

 printerHandle, err := OpenDefaultPrinter()
 if err != nil {panic}

 err = Print(printerHandle, text)
 if err != nil {panic(err)}

}

func OpenDefaultPrinter() (uintptr, error) {

     printerName, printerName16 := getDefaultPrinterName();     
     
     printerHandle, err := openPrinterFunc(printerName, printerName16)      
     if err != nil {return 0, err}
     
     return printerHandle, nil
}

func Print(printerHandle uintptr, text string) error {
     
     var err error

     startPrinter(printerHandle, path)
     startPagePrinter.Call(printerHandle)
     err = writePrinterFunc(printerHandle, text)
     endPagePrinter.Call(printerHandle)
     endDocPrinter.Call(printerHandle)
     
     return err
}

func writePrinterFunc(printerHandle uintptr, text string) error {

 bs := []byte(text)
     var contentLen = uintptr(uint32(len(bs)))
     var writtenLen int
     _, _, err = writePrinter.Call(printerHandle, uintptr(unsafe.Pointer(&bs)),  contentLen, uintptr(unsafe.Pointer(&writtenLen)))
     fmt.Println("Writing to printer:", err)

     return nil
}

Upvotes: -1

Related Questions