escpos

package module
v0.0.0-...-1cbc4c6 Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2019 License: MIT Imports: 22 Imported by: 0

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/epsimatic/escpos

Example epos-server

An example EPOS server implementation is available in original repository. That example server is more or less compatible with Epson TM-Intelligent printers and print server implementations.

Please note that it requires gokogiri, libxml and cgo to build

Usage

The escpos package can be used similarly to the following:

package main

import (
    "bufio"
	"log"
    "os"

    "github.com/epsimatic/escpos"
)

func main() {
	f, err := os.OpenFile("/dev/usb/lp0", os.O_WRONLY, 0644)
	println("Port opened")
	if err != nil {
		log.Fatal(err)
	}
	defer func() {
		if err := f.Close(); err != nil {
			log.Fatalf("Printer close failed: %v", err)
		}
	}()

	w := bufio.NewWriter(f)
	defer func() {
		if err := w.Flush(); err != nil {
			log.Fatalf("Print buffer flush failed: %v", err)
		}
	}()

	p, err := escpos.NewPrinter(w)
	if err != nil {
		log.Fatalf("Print init failed: %v", err)
	}
	defer p.End()
    p.Init()

	println("Printer opened")

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

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

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

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

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

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

    p.Cut()
}

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

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Alignment

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

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.Writer) (*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) 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: clears the data in the print buffer and resets the device mode to that in effect when power was turned on.

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

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

WriteData writes buf to printer.

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

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 certain 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) SetImageHeight

func (p *Printer) SetImageHeight(height 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(newWhiteOnBlack 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(str string) (int, error)

Write writes string to printer without Line Feed, so it's not printed out instantly

func (*Printer) WriteLn

func (p *Printer) WriteLn(str string) (int, error)

Write writes string 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) WriteRenderedString

func (p *Printer) WriteRenderedString(str string) (int, error)

WriteRenderedString renders string as image and outputs it to the printer.

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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