README
¶
About escpos
This is a Golang package that provides ESC-POS library functions to help with sending control codes to a ESC-POS thermal printer.
It was largely inspired by seer-robotics/escpos but is a complete rewrite.
It implements the protocol described in this Command Manual
Current featureset
- Initializing the Printer
- Toggling Underline mode
- Toggling Bold text
- Toggling upside-down character printing
- Toggling Reverse mode
- Linespace settings
- Rotated characters
- Align text
- Default ASCII Charset, Western Europe and GBK encoding
- Character size settings
- UPC-A, UPC-E, EAN13, EAN8 Barcodes
- QR Codes
- Standard printing mode
- Image Printing
- Printing of predefined NV images
Installation
Install the package via the following:
go get -u github.com/hennedo/escpos
Usage
The escpos package can be used as the following:
package main
import (
"github.com/hennedo/escpos"
"net"
)
func main() {
socket, err := net.Dial("tcp", "192.168.8.40:9100")
if err != nil {
println(err.Error())
}
defer socket.Close()
p := escpos.New(socket)
p.SetConfig(escpos.ConfigEpsonTMT20II)
p.Bold(true).Size(2, 2).Write("Hello World")
p.LineFeed()
p.Bold(false).Underline(2).Justify(escpos.JustifyCenter).Write("this is underlined")
p.LineFeed()
p.QRCode("https://github.com/hennedo/escpos", true, 10, escpos.QRCodeErrorCorrectionLevelH)
// You need to use either p.Print() or p.PrintAndCut() at the end to send the data to the printer.
p.PrintAndCut()
}
Disable features
As the library sets all the styling parameters again for each call of Write, you might run into compatibility issues. Therefore it is possible to deactivate features. To do so, use a predefined config (available for all printers listed under Compatibility) right after the escpos.New call
p := escpos.New(socket)
p.SetConfig(escpos.ConfigEpsonTMT20II) // predefined config for the Epson TM-T20II
// or for example
p.SetConfig(escpos.PrinterConfig(DisableUnderline: true))
Compatibility
This is a (not complete) list of supported and tested devices.
Manufacturer | Model | Styling | Barcodes | QR Codes | Images |
---|---|---|---|---|---|
Epson | TM-T20II | ✅ | ✅ | ✅ | ✅ |
Epson | TM-T88II | ☑️ UpsideDown Printing not supported |
✅ | ✅ | |
Xprinter | T80Q | ✅ | ✅ | ✅ | ✅ |
Documentation
¶
Overview ¶
stolen and modified from https://github.com/mugli/png2escpos
Index ¶
- Constants
- Variables
- type Escpos
- func (e *Escpos) BarcodeHeight(p uint8) (int, error)
- func (e *Escpos) BarcodeWidth(p uint8) (int, error)
- func (e *Escpos) Bold(p bool) *Escpos
- func (e *Escpos) Codabar(code string) (int, error)
- func (e *Escpos) Code128(code string) (int, error)
- func (e *Escpos) Code39(code string) (int, error)
- func (e *Escpos) Code93(code string) (int, error)
- func (e *Escpos) Cut() (int, error)
- func (e *Escpos) DefaultLineSpacing() (int, error)
- func (e *Escpos) DisableKanjiMode() (int, error)
- func (e *Escpos) EAN13(code string) (int, error)
- func (e *Escpos) EAN8(code string) (int, error)
- func (e *Escpos) HRIFont(p bool) (int, error)
- func (e *Escpos) HRIPosition(p uint8) (int, error)
- func (e *Escpos) ITF(code string) (int, error)
- func (e *Escpos) Initialize() (int, error)
- func (e *Escpos) Justify(p uint8) *Escpos
- func (e *Escpos) LineFeed() (int, error)
- func (e *Escpos) LineFeedD(p uint8) (int, error)
- func (e *Escpos) LineSpacing(p uint8) (int, error)
- func (e *Escpos) MotionUnits(x, y uint8) (int, error)
- func (e *Escpos) Print() error
- func (e *Escpos) PrintAndCut() error
- func (e *Escpos) PrintImage(image image.Image) (int, error)
- func (e *Escpos) PrintNVBitImage(p uint8, mode uint8) (int, error)
- func (e *Escpos) QRCode(code string, model bool, size uint8, correctionLevel uint8) (int, error)
- func (e *Escpos) Reverse(p bool) *Escpos
- func (e *Escpos) Rotate(p bool) *Escpos
- func (e *Escpos) SelectCodePage(codePage uint8) (int, error)
- func (e *Escpos) SetConfig(conf PrinterConfig)
- func (e *Escpos) Size(width uint8, height uint8) *Escpos
- func (e *Escpos) UPCA(code string) (int, error)
- func (e *Escpos) UPCE(code string) (int, error)
- func (e *Escpos) Underline(p uint8) *Escpos
- func (e *Escpos) UpsideDown(p bool) *Escpos
- func (e *Escpos) Write(data string) (int, error)
- func (e *Escpos) WriteRaw(data []byte) (int, error)
- func (e *Escpos) WriteWEU(data string) (int, error)
- type PrinterConfig
- type Style
Constants ¶
Variables ¶
var ( ConfigEpsonTMT20II = PrinterConfig{} ConfigEpsonTMT88II = PrinterConfig{DisableUpsideDown: true} ConfigSOL802 = PrinterConfig{DisableUpsideDown: true} ConfigXprinterT80Q = PrinterConfig{} )
Functions ¶
This section is empty.
Types ¶
type Escpos ¶
type Escpos struct { Style Style // contains filtered or unexported fields }
func (*Escpos) BarcodeHeight ¶
Sets the height for a bar code. Default is 162.
func (*Escpos) BarcodeWidth ¶
Sets the horizontal size for a bar code. Default is 3. Must be between 2 and 6
func (*Escpos) Codabar ¶
Prints a CODABAR Barcode. code can contain the characters 0-9, A-D, a-d, $, +, -, ., /
func (*Escpos) Code128 ¶
Prints a CODE128 Barcode. code can contain the characters 0-9, A-D, a-d, $, +, -, ., /
func (*Escpos) Code39 ¶
Prints a CODE39 Barcode. code can contain the characters 0-9, A-Z, space, $, %, +, -, ., /
func (*Escpos) Code93 ¶
Print a CODE93 Barcode. code can contain the characters 0-9, A-Z, space, $, %, +, -, ., /
func (*Escpos) Cut ¶
Feeds the paper to the end and performs a Cut. In the ESC/POS Command Manual there is also PartialCut and FullCut documented, but it does exactly the same.
func (*Escpos) DefaultLineSpacing ¶
Sets the line spacing to the default. According to command manual this is 1/6 inch
func (*Escpos) DisableKanjiMode ¶
Disables Kanji mode on the printer
func (*Escpos) EAN13 ¶
Prints a EAN13 Barcode. code can only be numerical characters and must have a length of 12 or 13
func (*Escpos) EAN8 ¶
Prints a EAN8 Barcode. code can only be numerical characters and must have a length of 7 or 8
func (*Escpos) HRIPosition ¶
Sets the position of the HRI characters 0: Not Printed 1: Above the bar code 2: Below the bar code 3: Both
func (*Escpos) ITF ¶
Prints a ITF Barcode. code can only contain numerical characters and must have an even length
func (*Escpos) Initialize ¶
Initializes the printer to the settings it had when turned on
func (*Escpos) Justify ¶
Sets the justification of the text. Possible values are 0, 1 or 2. You can use JustifyLeft for left alignment JustifyCenter for center alignment JustifyRight for right alignment
func (*Escpos) LineFeedD ¶
According to command manual this prints and feeds the paper p*line spacing.
func (*Escpos) LineSpacing ¶
Sets the line spacing to multiples of the "horizontal and vertical motion units".. Those can be set with MotionUnits
func (*Escpos) MotionUnits ¶
Sets the horizontal (x) and vertical (y) motion units to 1/x inch and 1/y inch. Well... According to the manual anyway. You may not want to use this, as it does not seem to do the same on an Epson TM-20II
func (*Escpos) PrintAndCut ¶
Sends the buffered data to the printer and performs a cut
func (*Escpos) PrintImage ¶
Prints an image
func (*Escpos) PrintNVBitImage ¶
Print a predefined bit image with index p and mode mode
func (*Escpos) QRCode ¶
Prints a QR Code. code specifies the data to be printed model specifies the qr code model. false for model 1, true for model 2 size specifies the size in dots. It needs to be between 1 and 16
func (*Escpos) Reverse ¶
Sets Reverse printing. If true the printer will inverse to white text on black background.
func (*Escpos) SelectCodePage ¶
Selects code page
func (*Escpos) Size ¶
Sets the size of the font. Width and Height should be between 0 and 5. If the value is bigger than 5, 5 is used.
func (*Escpos) UPCA ¶
Prints a UPCA Barcode. code can only be numerical characters and must have a length of 11 or 12
func (*Escpos) UPCE ¶
Prints a UPCE Barcode. code can only be numerical characters and must have a length of 11 or 12
func (*Escpos) Underline ¶
Sets the Underline. p can be 0, 1 or 2. It defines the thickness of the underline in dots