thermalize

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2024 License: MIT Imports: 6 Imported by: 0

README

thermalize

thermalize is a library that generates commands for thermal printers.

logo.png

Installation

thermalize can be installed like any other Go library through go get:

go get github.com/gromey/thermalize

Or, if you are already using Go Modules, you may specify a version number as well:

go get github.com/gromey/thermalize@latest

Getting Started

Example 1 (ESC/POS)
package main

import (
	"bytes"

	"github.com/gromey/thermalize"
	"github.com/phin1x/go-ipp"
)

func main() {
	w := new(bytes.Buffer)

	p := thermalize.NewEscape(48, 576, w, false)

	p.Init()
	p.CodePage(16)
	p.LineFeed()
	p.Align(thermalize.Center)
	p.Bold(true)
	p.Text("Hello world!", nil)
	p.LineFeed()
	p.FullCut()

	defer w.Reset()

	doc := ipp.Document{
		Document: w,
		Size:     w.Len(),
		Name:     "Test Page",
		MimeType: ipp.MimeTypeOctetStream,
	}

	client := ipp.NewIPPClient("localhost", 631, "", "", true)
	
	if _, err := client.PrintJob(doc, "your_printer_name", nil); err != nil {
		panic(err)
    }
}

Example 2 (Star)
package main

import (
	"bufio"
	"os"

	"github.com/gromey/thermalize"
)

func main() {
	f, err := os.OpenFile("/dev/ttyUSB0", os.O_RDWR, 0755)
	if err != nil {
		panic(err)
	}
	defer func() { _ = f.Close() }()

	w := bufio.NewWriter(f)

	p := thermalize.NewStar(48, 576, w, false)

	p.Init()
	p.CodePage(32)
	p.LineFeed()
	p.Align(thermalize.Center)
	p.Bold(true)
	p.Text("Hello world!", nil)
	p.LineFeed()
	p.FullCut()

	if err = w.Flush(); err != nil {
		panic(err)
	}
}

Example 3 (Image)
package main

import (
	"bufio"
	"os"

	"github.com/gromey/thermalize"
)

func main() {
	f, err := os.OpenFile("/dev/ttyUSB0", os.O_RDWR, 0755)
	if err != nil {
		panic(err)
	}
	defer func() { _ = f.Close() }()
	
	w := bufio.NewWriter(f)

	p := thermalize.NewEscape(48, 576, w, false)

	// To change the level of gray that should be visible when printing, change GrayLevel setting.
	// Default is 127.
	thermalize.GrayLevel = 150

	p.Init()
	p.LineFeed()
	p.Align(thermalize.Center)

	// Before you send an image to print, you must ensure
	// that its width is less than or equal to the width of the print area.
	p.Image(thermalize.Logo(), false)
	p.LineFeed()
	p.FullCut()

	if err = w.Flush(); err != nil {
		panic(err)
	}
}

Documentation

Index

Constants

View Source
const (
	NUL byte = iota
	SOH
	STX
	ETX
	EOT
	ENQ
	ACK
	BEL
	BS
	HT
	LF
	VT
	FF
	CR
	SO
	SI
	DLE
	DC1
	DC2
	DC3
	DC4
	NAK
	SYN
	ETB
	CAN
	EM
	SUB
	ESC
	FS
	GS
	RS
	US
	NIL
)
View Source
const (
	Left = iota
	Center
	Right
)
View Source
const (
	NoUnderling = iota
	OneDotUnderling
	TwoDotsUnderling
)
View Source
const (
	HRIFontA = iota // (12 x 24)
	HRIFontB        // (9 x 17)
)
View Source
const (
	HRINotPrinted = iota
	HRIAbove
	HRIBelow
	HRIAboveAndBelow
)
View Source
const (
	UpcA = iota
	UpcE
	JanEAN8
	JanEAN13
	Code39
	Code93
	Code128
	ITF
	NW7
	GS1128
	GS1Omnidirectional
	GS1Truncated
	GS1Limited
	GS1Expanded
)
View Source
const (
	L = iota // L recovers 7% of data
	M        // M recovers 15% of data
	Q        // Q recovers 25% of data
	H        // H recovers 30% of data
)
View Source
const (
	DrawerPin2 = iota
	DrawerPin5
)

Variables

View Source
var GrayLevel = defaultGrayLevel

GrayLevel the level of gray that should be visible when printing.

Functions

func Gray

func Gray(c color.Color, invert bool) bool

func ImageToBin

func ImageToBin(img image.Image, invert bool) (int, []byte)

func ImageToBit

func ImageToBit(img image.Image, invert bool) (int, []byte)
func Logo() (img image.Image)

Logo returns the library logo.

Types

type Cmd

type Cmd interface {
	// CPL returns the number of characters per line specified when the command set was initialized.
	CPL() int

	// PPL returns the number of pixel per line specified when the command set was initialized.
	PPL() int

	// Write writes raw bytes.
	// If a writer is not provided or an error occurs during writing, it will panic.
	Write(bs ...byte)

	// Text adds printable string along with encoding, if an encoder is provided.
	//
	// Since Golang uses UTF-8 character encoding by default, you must provide an encoder
	// to convert the string according to the specified code page.
	//
	// If the encoder is not provided, the text will be printed using the default UTF-8 encoding,
	// which may result in incorrect printing.
	Text(s string, enc func(string) []byte)

	// Init initializes printer.
	// Clears the data in the print buffer and resets the printer modes.
	Init()

	// LeftMargin sets left margin.
	LeftMargin(n int)

	// WidthArea sets print area width.
	WidthArea(n int)

	// AbsolutePosition sets absolute print position.
	AbsolutePosition(n int)

	// Align specifies position alignment.
	//
	//	b = 0, left justification enabled;
	//	b = 1, center justification enabled;
	//	b = 2, right justification enabled.
	Align(b byte)

	// TabPositions sets horizontal tab position.
	// Default 8, 16, 24, 32, 40, ..., 232, 240, 248
	TabPositions(bs ...byte)

	// Tab moves the print position to the next horizontal tab position.
	// Default 8, 16, 24, 32, 40, ..., 232, 240, 248
	Tab()

	// CodePage selects character code table.
	CodePage(b byte)

	// CharSize selects character width and height.
	CharSize(w byte, h byte)

	// Bold selects emphasized printing.
	Bold(b bool)

	// Underling selects/cancels underling mode.
	//
	//	b = 0, underline mode disabled;
	//	b = 1, underline mode (1-dot thick) enabled;
	//	b = 2, underline mode (2-dot thick) enabled.
	Underling(b byte)

	// BarcodeWidth sets the 1D barcode width multiplier.
	BarcodeWidth(b byte)

	// BarcodeHeight sets the 1D barcode height, measured in dots.
	//
	//	1 <= b <= 255.
	BarcodeHeight(b byte)

	// HRIFont selects HRI character font.
	//
	//	b = 0, font A (12 x 24);
	//	b = 1, font B (9 x 17).
	HRIFont(b byte)

	// HRIPosition selects HRI character print position.
	//
	//	b = 0, not printed;
	//	b = 1, above the barcode;
	//	b = 2, below the barcode;
	//	b = 3, above and below the barcode.
	HRIPosition(b byte)

	// Barcode adds a barcode to print.
	//
	//	m = 0, UpcA;
	//	m = 1, UpcE;
	//	m = 2, JanEAN8;
	//	m = 3, JanEAN13;
	//	m = 4, Code39;
	//	m = 5, Code93;
	//	m = 6, Code128;
	//	m = 7, ITF;
	//	m = 8, NW7;
	//	m = 9, GS1128;
	//	m = 10, GS1Omnidirectional;
	//	m = 11, GS1Truncated;
	//	m = 12, GS1Limited;
	//	m = 13, GS1Expanded.
	// If m is out of range, Code39 will be used by default.
	Barcode(m byte, s string)

	// QRCodeSize sets the size of module.
	QRCodeSize(b byte)

	// QRCodeCorrectionLevel sets the correction level.
	//
	//	b = 0, correction level 7 %;
	//	b = 1, correction level 15 %;
	//	b = 2, correction level 25 %;
	//	b = 3, correction level 30 %.
	QRCodeCorrectionLevel(b byte)

	// QRCode adds a QR code to print.
	QRCode(s string)

	// Image adds an image to print.
	Image(img image.Image, invert bool)

	// Feed prints current buffer and executes n/4mm paper feed.
	//
	//	0 <= b <= 255.
	Feed(b byte)

	// LineFeed prints the data in the print buffer and feeds one line, based on the current line spacing.
	LineFeed()

	// Cut executes the auto-cutter.
	Cut(m byte, p byte)

	// FullCut executes the auto-cutter across the full width of the paper.
	FullCut()

	// OpenCashDrawer generates pulse to open a cache drawer.
	OpenCashDrawer(m byte, t1 byte, t2 byte)
}

func NewEscape

func NewEscape(cpl, ppl int, w io.Writer, obsolete bool) Cmd

NewEscape returns the most popular set of printer commands. If the obsolete is true, the obsolete print image command will be used.

func NewSkipper

func NewSkipper(cpl, ppl int, w io.Writer) Cmd

NewSkipper returns a set of methods that skip the execution of unimplemented commands. This writes raw bytes and text to a writer.

func NewStar

func NewStar(cpl, ppl int, w io.Writer, _ bool) Cmd

NewStar returns the star set of printer commands.

Jump to

Keyboard shortcuts

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