split big chunk of data to write to printer
This commit is contained in:
34
printer.go
34
printer.go
@@ -17,7 +17,9 @@ import (
|
||||
|
||||
"github.com/kenshaw/escpos"
|
||||
)
|
||||
import "errors"
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
//export PrintImg
|
||||
func PrintImg(printer *C.char, imagePath *C.char) *C.char {
|
||||
@@ -87,10 +89,17 @@ func PrintImg(printer *C.char, imagePath *C.char) *C.char {
|
||||
prt.SetSmooth(1)
|
||||
prt.SetAlign("left")
|
||||
|
||||
err := printImg(prt, goImagePath)
|
||||
imgBytes, err := imgData(goImagePath)
|
||||
if err != nil {
|
||||
return NewErr(err)
|
||||
}
|
||||
err = WriteChunk(w, prt, imgBytes)
|
||||
if err != nil {
|
||||
return NewErr(err)
|
||||
}
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
w.Flush()
|
||||
|
||||
prt.WriteRaw([]byte{0x1B, 0x64, 0x03})
|
||||
prt.Cut()
|
||||
prt.End()
|
||||
@@ -104,3 +113,24 @@ func Print(printer, imagePath string) string {
|
||||
r := C.GoString(result)
|
||||
return r
|
||||
}
|
||||
|
||||
// WriteChunk breaks the large byte array into smaller chunks
|
||||
func WriteChunk(w *bufio.ReadWriter, prt *escpos.Escpos, data []byte) error {
|
||||
chunkSize := 1024 // 1 KB chunks work best for network printers
|
||||
for i := 0; i < len(data); i += chunkSize {
|
||||
end := min(i+chunkSize, len(data))
|
||||
|
||||
_, err := prt.WriteRaw(data[i:end])
|
||||
if err != nil {
|
||||
log.Println("Error writing image chunk:", err)
|
||||
return err
|
||||
}
|
||||
if err := w.Flush(); err != nil {
|
||||
log.Println("Error flushing buffer:", err)
|
||||
return err
|
||||
}
|
||||
// Give the printer's network chip a microsecond to breathe
|
||||
time.Sleep(5 * time.Millisecond)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user