escpos

package module
v0.0.0-...-d373496 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2021 License: MIT Imports: 28 Imported by: 2

README

About escpos

This is a simple Go package that provides ESC-POS library functions to help with sending control codes to a ESC-POS capable printer such as an Epson TM-T82 or similar.

These printers are often used in retail environments in conjunction with a point-of-sale (POS) system.

Installation

Install the package via the following:

go get -u github.com/cloudinn/escpos

Example epos-server

An example EPOS server implementation is available in the cmd/epos-server subdirectory of this project. This example server is more or less compatible with Epson TM-Intelligent printers and print server implementations.

Usage

The escpos package can be used similarly to the following:

package main

import (
    "bufio"
    "os"

    "github.com/cloudinn/escpos"
)

func main() {
    f, err := os.Open("/dev/usb/lp0")
    if err != nil {
        panic(err)
    }
    defer f.Close()

    w := bufio.NewWriter(f)
    p := escpos.New(w)

    p.Init()
    p.SetSmooth(1)
    p.SetFontSize(2, 3)
    p.SetFont("A")
    p.Write("test ")
    p.SetFont("B")
    p.Write("test2 ")
    p.SetFont("C")
    p.Write("test3 ")
    p.Formfeed()

    p.SetFont("B")
    p.SetFontSize(1, 1)

    p.SetEmphasize(1)
    p.Write("halle")
    p.Formfeed()

    p.SetUnderline(1)
    p.SetFontSize(4, 4)
    p.Write("halle")

    p.SetReverse(1)
    p.SetFontSize(2, 4)
    p.Write("halle")
    p.Formfeed()

    p.SetFont("C")
    p.SetFontSize(8, 8)
    p.Write("halle")
    p.FormfeedN(5)

    p.Cut()
    p.End()

    w.Flush()
}

NOTE

The Imported font inside the code is a system font called DejaVuSansMono-Bold.ttfsoyou shoukd make sure it exists in the system and it'splaced in the "/usr/share/fonts/truetype/dejavu/"

TODO

  • Fix barcode support

Credits

Documentation

Index

Constants

View Source
const (
	// DefaultEndpoint is the default server endpoint for ePOS printers.
	DefaultEndpoint = "/cgi-bin/epos/service.cgi"
)

Variables

View Source
var (
	// ErrBodyElementEmpty is the body element empty error.
	ErrBodyElementEmpty = errors.New("Body element empty")
)

Functions

This section is empty.

Types

type Printer

type Printer struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Printer wraps sending ESC-POS commands to a io.Writer.

func NewPrinter

func NewPrinter(w io.ReadWriter) (*Printer, error)

NewPrinter creates a new printer using the specified writer.

func (*Printer) Barcode

func (p *Printer) Barcode(barcode string, format int)

Barcode sends a barcode to the printer.

func (*Printer) Cash

func (p *Printer) Cash()

Cash writes the cash code to the printer.

func (*Printer) CloseConnection

func (p *Printer) CloseConnection() error

func (*Printer) Cut

func (p *Printer) Cut()

Cut writes the cut code to the printer.

func (*Printer) End

func (p *Printer) End()

End terminates the printer session.

func (*Printer) Feed

func (p *Printer) Feed(params map[string]string) error

Feed feeds the printer, applying the supplied params as necessary.

func (*Printer) FeedAndCut

func (p *Printer) FeedAndCut(params map[string]string)

FeedAndCut feeds the printer using the supplied params and then sends a cut command.

func (*Printer) Formfeed

func (p *Printer) Formfeed()

Formfeed writes 1 formfeed to the printer.

func (*Printer) FormfeedN

func (p *Printer) FormfeedN(n int)

FormfeedN writes N formfeeds to the printer.

func (*Printer) Image

func (p *Printer) Image(params map[string]string, data string) error

Image writes an image using the supplied params.

func (*Printer) Init

func (p *Printer) Init()

Init resets the state of the printer, and writes the initialize code.

func (*Printer) Linefeed

func (p *Printer) Linefeed()

Linefeed writes a line end to the printer.

func (*Printer) PrintImage

func (p *Printer) PrintImage(imgPath string) error

PrintImage Print Image

func (*Printer) PrintTextImage

func (p *Printer) PrintTextImage(text string) error

PrintTextImage takes a string convert it to an image and print it

func (*Printer) Pulse

func (p *Printer) Pulse()

Pulse sends the pulse (open drawer) code to the printer.

func (*Printer) Raster

func (p *Printer) Raster(width, height, lineWidth int, imgBw []byte, printingType string)

Raster writes a rasterized version of a black and white image to the printer with the specified width, height, and lineWidth bytes per line.

func (*Printer) ReadStatus

func (p *Printer) ReadStatus() bool

func (*Printer) Reset

func (p *Printer) Reset()

Reset resets the printer state.

func (*Printer) SendEmphasize

func (p *Printer) SendEmphasize()

SendEmphasize sends the emphasize / doublestrike command to the printer.

func (*Printer) SendFontSize

func (p *Printer) SendFontSize()

SendFontSize sends the font size command to the printer.

func (*Printer) SendMoveX

func (p *Printer) SendMoveX(x uint16)

SendMoveX sends the move x command to the printer.

func (*Printer) SendMoveY

func (p *Printer) SendMoveY(y uint16)

SendMoveY sends the move y command to the printer.

func (*Printer) SendReverse

func (p *Printer) SendReverse()

SendReverse sends the reverse command to the printer.

func (*Printer) SendRotate

func (p *Printer) SendRotate()

SendRotate sends the rotate command to the printer.

func (*Printer) SendSmooth

func (p *Printer) SendSmooth()

SendSmooth sends the smooth command to the printer.

func (*Printer) SendUnderline

func (p *Printer) SendUnderline()

SendUnderline sends the underline command to the printer.

func (*Printer) SendUpsidedown

func (p *Printer) SendUpsidedown()

SendUpsidedown sends the upsidedown command to the printer.

func (*Printer) SetAlign

func (p *Printer) SetAlign(align string)

SetAlign sets the alignment state and sends it to the printer.

func (*Printer) SetDPI

func (p *Printer) SetDPI(resolution float64)

SetDPI sets resolution in dots per inch for the image

func (*Printer) SetEmphasize

func (p *Printer) SetEmphasize(u byte)

SetEmphasize sets the emphasize state and sends it to the printer.

func (*Printer) SetFont

func (p *Printer) SetFont(font string)

SetFont sets the font on the printer.

func (*Printer) SetFontFile

func (p *Printer) SetFontFile(filepath string)

SetFontFile to choose a certien font to print the image with

func (*Printer) SetFontSize

func (p *Printer) SetFontSize(width, height byte)

SetFontSize sets the font size state and sends the command to the printer.

func (*Printer) SetFontSizePoints

func (p *Printer) SetFontSizePoints(fontSize float64)

SetFontSizePoint sets font size in points for some selected font

func (*Printer) SetHinting

func (p *Printer) SetHinting(hintingVal string)

SetHinting sets hinting

func (*Printer) SetImageHight

func (p *Printer) SetImageHight(hight int)

func (*Printer) SetLang

func (p *Printer) SetLang(lang string)

SetLang sets the language state and sends it to the printer.

func (*Printer) SetReverse

func (p *Printer) SetReverse(v byte)

SetReverse sets the reverse state and sends it to the printer.

func (*Printer) SetRotate

func (p *Printer) SetRotate(v byte)

SetRotate sets the rotate state and sends it to the printer.

func (*Printer) SetSmooth

func (p *Printer) SetSmooth(v byte)

SetSmooth sets the smooth state and sends it to the printer.

func (*Printer) SetSpacing

func (p *Printer) SetSpacing(spacingVal float64)

SetSpacing set spacing between lines in image

func (*Printer) SetUnderline

func (p *Printer) SetUnderline(v byte)

SetUnderline sets the underline state and sends it to the printer.

func (*Printer) SetUpsidedown

func (p *Printer) SetUpsidedown(v byte)

SetUpsidedown sets the upsidedown state and sends it to the printer.

func (*Printer) SetWhiteOnBlack

func (p *Printer) SetWhiteOnBlack(wonbVal bool)

SetWhiteOnBlack sets the background for the image to white for true or black for false

func (*Printer) Text

func (p *Printer) Text(params map[string]string, text string) error

Text sends a block of text to the printer using the formatting parameters in params.

func (*Printer) TextToRaster

func (p *Printer) TextToRaster(text string, fontSize float64, wb bool) (data []byte, width int, height int, err error)

TextToRaster takes a string, font size, boolean value if true will print text black background white if false will print text white background black return slice bytes of raster image with width and height

func (*Printer) Write

func (p *Printer) Write(buf []byte) (int, error)

Write writes buf to printer.

func (*Printer) WriteNode

func (p *Printer) WriteNode(name string, params map[string]string, data string)

WriteNode writes a node of type name with the supplied params and data to the printer.

func (*Printer) WriteString

func (p *Printer) WriteString(s string) (int, error)

WriteString writes a string to the printer.

type Server

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

Server wrap

func NewServer

func NewServer(w io.ReadWriter, opts ...ServerOption) (*Server, error)

NewServer creates a new ePOS server.

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(res http.ResponseWriter, req *http.Request)

ServeHTTP handles OPTIONS, Origin, and POST for an ePOS server.

type ServerOption

type ServerOption func(*Server) error

ServerOption is a server option.

func WithLog

func WithLog(f func(string, ...interface{})) ServerOption

WithLog is a server option to set a logging func.

Directories

Path Synopsis
cmd
example

Jump to

Keyboard shortcuts

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