escpos

package module
v0.0.0-...-67b2918 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 25, 2024 License: MIT Imports: 11 Imported by: 8

README

go-escpos

This is a Go library to talk to escpos capable devices, mainly the Epson POS printers.

it will allow you to send print jobs in a simple functional interface, this library also supports automatic discovery of USB devices.

This library works with both Go and TinyGo (for microcontrollers).

Documentation can be found on pkg.go.dev

Example

package main

import (
	"fmt"

	"github.com/mect/go-escpos"
)

func main() {
	p, err := escpos.NewUSBPrinterByPath("") // empry string will do a self discovery
	if err != nil {
		fmt.Println(err)
		return
	}

	p.Init() // start
	p.Smooth(true) // use smootth printing
	p.Size(2, 2) // set font size
	p.PrintLn("HELLO GO")

	p.Size(1, 1)
	p.Font(escpos.FontB) // change font
	p.PrintLn("This is a test of MECT go-escpos")
	p.Font(escpos.FontA)

	p.Align(escpos.AlignRight) // change alignment
	p.PrintLn("An all Go\neasy to use\nEpson POS Printer library")
	p.Align(escpos.AlignLeft)

	p.Size(2, 2)
	p.PrintLn("* No magic numbers")
	p.PrintLn("* ISO8859-15 ŠÙþþØrt")
	p.Underline(true)
	p.PrintLn("* Extended layout")
	p.Underline(false)
	p.PrintLn("* All in Go!")

	p.Align(escpos.AlignCenter)
	p.Barcode("MECT", escpos.BarcodeTypeCODE39) // print barcode
	p.Align(escpos.AlignLeft)

	p.FeedN(2) // feed 2
	p.Cut() // cut
	p.End() // stop
}

(The TinyGo example can be found in demo-tinygo)

Limitations

  • The USB discovery only works on Linux due to the use of udev

(PRs to fix these welcome!)

Known to work with

  • Epson TM-20III

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrorNoDevicesFound = errors.New("No devices found")

Functions

This section is empty.

Types

type Alignment

type Alignment uint8

Alignment defines the text blignment

const (
	AlignLeft   Alignment = 0
	AlignCenter Alignment = 1
	AlignRight  Alignment = 2
)

type BarcodeType

type BarcodeType string

BarcodeType defines the type of barcode

const (
	// function type A
	BarcodeTypeUPCA    BarcodeType = "\x00"
	BarcodeTypeUPCE    BarcodeType = "\x01"
	BarcodeTypeEAN13   BarcodeType = "\x02"
	BarcodeTypeEAN8    BarcodeType = "\x03"
	BarcodeTypeCODE39  BarcodeType = "\x04"
	BarcodeTypeITF     BarcodeType = "\x05"
	BarcodeTypeCODABAR BarcodeType = "\x06"

	// function type B
	BarcodeTypeCODE128 BarcodeType = "\x49"
)

type ErrorStatus

type ErrorStatus byte
const (
	ErrorNone      ErrorStatus = 0x00
	ErrorCoverOpen ErrorStatus = 0x04
	ErrorPaperOut  ErrorStatus = 0x20
	ErrorGeneric   ErrorStatus = 0x40
)

type Font

type Font uint8

Font defined the font type

const (
	FontA Font = 0
	FontB Font = 1
	FontC Font = 2
)

type Printer

type Printer struct {
	// contains filtered or unexported fields
}

func NewPrinterByRW

func NewPrinterByRW(rwc io.ReadWriteCloser) (*Printer, error)

func NewUSBPrinterByPath

func NewUSBPrinterByPath(devpath string) (*Printer, error)

NewUSBPrinter returns a new printer with a USB Vendor and Product ID if both are 0 it will return the first found Epson POS printer

func (*Printer) Align

func (p *Printer) Align(align Alignment) error

Align will change the text alignment

func (*Printer) Aztec

func (p *Printer) Aztec(code string, size int) error

Aztec will print a Aztec code with given data, the size is between 2 and 16, if an invalid size is given it will default to 3

func (*Printer) AztecViaImage

func (p *Printer) AztecViaImage(data string, width, height int) error

AztecViaImage prints an Aztec code using the image system for longer data that is not possible to print directly

func (*Printer) Barcode

func (p *Printer) Barcode(barcode string, format BarcodeType) error

Barcode will print a barcode of a specified type as well as the text value

func (*Printer) Close

func (p *Printer) Close() error

Close closes the connection to the printer, all commands will not work after this

func (*Printer) Cut

func (p *Printer) Cut() error

Cut sends the command to cut the paper

func (*Printer) DataMatrix

func (p *Printer) DataMatrix(code string, size int) error

DataMatrix will print a DataMatrix code with given data, the size is between 2 and 16, if an invalid size is given it will default to 3

func (*Printer) End

func (p *Printer) End() error

End sends an end signal to finalize the print job

func (*Printer) Feed

func (p *Printer) Feed(n int) error

Feed sends a paper feed command for a specified length

func (*Printer) Font

func (p *Printer) Font(font Font) error

Font changest the font face

func (*Printer) GetErrorStatus

func (p *Printer) GetErrorStatus() (ErrorStatus, error)

func (*Printer) Image

func (p *Printer) Image(img image.Image) error

Image prints a raster image

The image must be narrower than the printer's pixel width

func (*Printer) Init

func (p *Printer) Init() error

Init sends an init signal

func (*Printer) PDF417

func (p *Printer) PDF417(code string, size int) error

PDF417 will print a PDF417 code with given data, the size is between 2 and 16, if an invalid size is given it will default to 3

func (*Printer) Print

func (p *Printer) Print(data string) error

Print prints a string the data is re-encoded from Go's UTF-8 to ISO8859-15

func (*Printer) PrintAreaWidth

func (p *Printer) PrintAreaWidth(width int) error

PrintAreaWidth will set the print area width, by default it is the maximum. Eg. 380 is handy for less wide receipts used by card terminals

func (*Printer) PrintLn

func (p *Printer) PrintLn(data string) error

PrintLn does a Print with a newline attached

func (*Printer) QR

func (p *Printer) QR(code string, size int) error

QR will print a QR code with given data, the size is between 2 and 16, if an invalid size is given it will default to 3

func (*Printer) Size

func (p *Printer) Size(width, height uint8) error

Size changes the font size

func (*Printer) Smooth

func (p *Printer) Smooth(enabled bool) error

Smooth will enable or disable smooth text printing

func (*Printer) Underline

func (p *Printer) Underline(enabled bool) error

Underline will enable or disable underlined text

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL