escpos

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2022 License: MIT Imports: 15 Imported by: 0

README

About escpos GoDoc

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

Documentation

Overview

stolen and modified from https://github.com/mugli/png2escpos

Index

Constants

View Source
const (
	JustifyLeft                 uint8 = 0
	JustifyCenter               uint8 = 1
	JustifyRight                uint8 = 2
	QRCodeErrorCorrectionLevelL uint8 = 48
	QRCodeErrorCorrectionLevelM uint8 = 49
	QRCodeErrorCorrectionLevelQ uint8 = 50
	QRCodeErrorCorrectionLevelH uint8 = 51
)
View Source
const (
	BarcodeUPCA    byte = 0
	BarcodeUPCE    byte = 1
	BarcodeJAN13   byte = 2
	BarcodeJAN8    byte = 3
	BarcodeCODE39  byte = 4
	BarcodeITF     byte = 5
	BarcodeCODABAR byte = 6
)

Variables

View Source
var (
	ConfigEpsonTMT20II = PrinterConfig{}
	ConfigEpsonTMT88II = PrinterConfig{DisableUpsideDown: true}
	ConfigSOL802       = PrinterConfig{DisableUpsideDown: true}
)

Functions

func FromUTF8 added in v1.0.3

func FromUTF8(to string, s []byte) ([]byte, error)

Stuff for writing text. FromUTF8 convert from UTF-8 encoding to CJK encoding

Types

type Escpos

type Escpos struct {
	Style Style
	// contains filtered or unexported fields
}

func New

func New(dst io.Writer) (e *Escpos)

New create an Escpos printer

func (*Escpos) BarcodeHeight

func (e *Escpos) BarcodeHeight(p uint8) (int, error)

Sets the height for a bar code. Default is 162.

func (*Escpos) BarcodeWidth

func (e *Escpos) BarcodeWidth(p uint8) (int, error)

Sets the horizontal size for a bar code. Default is 3. Must be between 2 and 6

func (*Escpos) Bold

func (e *Escpos) Bold(p bool) *Escpos

Sets the printer to print Bold text.

func (*Escpos) CODE39 added in v1.0.2

func (e *Escpos) CODE39(code string) (int, error)

func (*Escpos) Cut

func (e *Escpos) Cut() (int, error)

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

func (e *Escpos) DefaultLineSpacing() (int, error)

Sets the line spacing to the default. According to command manual this is 1/6 inch

func (*Escpos) EAN13

func (e *Escpos) EAN13(code string) (int, error)

Prints a EAN13 Barcode. code can only be numerical characters and must have a length of 12 or 13

func (*Escpos) EAN8

func (e *Escpos) EAN8(code string) (int, error)

Prints a EAN8 Barcode. code can only be numerical characters and must have a length of 7 or 8

func (*Escpos) HRIFont

func (e *Escpos) HRIFont(p bool) (int, error)

Sets the HRI font to either false: Font A (12x24) or true: Font B (9x24)

func (*Escpos) HRIPosition

func (e *Escpos) HRIPosition(p uint8) (int, error)

Sets the position of the HRI characters 0: Not Printed 1: Above the bar code 2: Below the bar code 3: Both

func (*Escpos) Initialize

func (e *Escpos) Initialize() (int, error)

Initializes the printer to the settings it had when turned on

func (*Escpos) Justify

func (e *Escpos) Justify(p uint8) *Escpos

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) LineFeed

func (e *Escpos) LineFeed() (int, error)

Sends a newline to the printer.

func (*Escpos) LineFeedD

func (e *Escpos) LineFeedD(p uint8) (int, error)

According to command manual this prints and feeds the paper p*line spacing.

func (*Escpos) LineSpacing

func (e *Escpos) LineSpacing(p uint8) (int, error)

Sets the line spacing to multiples of the "horizontal and vertical motion units".. Those can be set with MotionUnits

func (*Escpos) MotionUnits

func (e *Escpos) MotionUnits(x, y uint8) (int, error)

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) Print

func (e *Escpos) Print() error

Sends the buffered data to the printer

func (*Escpos) PrintAndCut

func (e *Escpos) PrintAndCut() error

Sends the buffered data to the printer and performs a cut

func (*Escpos) PrintImage

func (e *Escpos) PrintImage(image image.Image) (int, error)

Prints an image

func (*Escpos) PrintNVBitImage

func (e *Escpos) PrintNVBitImage(p uint8, mode uint8) (int, error)

Print a predefined bit image with index p and mode mode

func (*Escpos) QRCode

func (e *Escpos) QRCode(code string, model bool, size uint8, correctionLevel uint8) (int, error)

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

func (e *Escpos) Reverse(p bool) *Escpos

Sets Reverse printing. If true the printer will inverse to white text on black background.

func (*Escpos) Rotate

func (e *Escpos) Rotate(p bool) *Escpos

Toggles 90° CW rotation

func (*Escpos) SetConfig

func (e *Escpos) SetConfig(conf PrinterConfig)

Sets the Printerconfig

func (*Escpos) Size

func (e *Escpos) Size(width uint8, height uint8) *Escpos

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

func (e *Escpos) UPCA(code string) (int, error)

Prints a UPCA Barcode. code can only be numerical characters and must have a length of 11 or 12

func (*Escpos) UPCE

func (e *Escpos) UPCE(code string) (int, error)

Prints a UPCE Barcode. code can only be numerical characters and must have a length of 11 or 12

func (*Escpos) Underline

func (e *Escpos) Underline(p uint8) *Escpos

Sets the Underline. p can be 0, 1 or 2. It defines the thickness of the underline in dots

func (*Escpos) UpsideDown

func (e *Escpos) UpsideDown(p bool) *Escpos

Toggles UpsideDown printing

func (*Escpos) Write

func (e *Escpos) Write(data string) (int, error)

Writes a string using the predefined options.

func (*Escpos) WriteGBK

func (e *Escpos) WriteGBK(data string) (int, error)

WriteGBK writes a string to the printer using GBK encoding

func (*Escpos) WriteRaw

func (e *Escpos) WriteRaw(data []byte) (int, error)

WriteRaw write raw bytes to the printer

func (*Escpos) WriteWEU

func (e *Escpos) WriteWEU(data string) (int, error)

WriteWEU writes a string to the printer using Western European encoding

type PrinterConfig

type PrinterConfig struct {
	DisableUnderline  bool
	DisableBold       bool
	DisableReverse    bool
	DisableRotate     bool
	DisableUpsideDown bool
	DisableJustify    bool
}

type Style

type Style struct {
	Bold          bool
	Width, Height uint8
	Reverse       bool
	Underline     uint8 // can be 0, 1 or 2
	UpsideDown    bool
	Rotate        bool
	Justify       uint8
}

Jump to

Keyboard shortcuts

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