thermalize

package module
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2024 License: MIT Imports: 9 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@latest

Getting Started

Example 1 (ESC/POS)
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)

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

	if err = w.Flush(); 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)

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

	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, thermalize.WithImageFuncVersion(1))

	// To change the level of gray that should be visible when printing, change GrayLevel setting.
	// Default is 127.
	thermalize.SetGrayLevel(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()
	p.Print()

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

Example 4 (Postscript)
package main

import (
	"bytes"
	"image"

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

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

	// m - Bar code mode.
	barCode := func(m byte, s string) image.Image {
		// get Bar code here.
		return nil
	}

	qrCode := func(s string) image.Image {
		// get QR code here.
		return nil
	}

	opts := []thermalize.Options{
		thermalize.WithPageHeight(5670),
		thermalize.WithBarCodeFunc(barCode),
		thermalize.WithQRCodeFunc(qrCode),
	}

	p := thermalize.NewPostscript(48, 576, w, opts...)

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

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

Documentation

Index

Constants

View Source
const (
	NUL byte = iota // NUL - Null.
	SOH             // SOH - Start of heading.
	STX             // STX - Start of text.
	ETX             // ETX - End of text.
	EOT             // EOT - End of transmission.
	ENQ             // ENQ - Enquiry.
	ACK             // ACK - Acknowledge.
	BEL             // BEL - Bell (beep).
	BS              // BS - Backspace.
	HT              // HT - Horizontal tabulation.
	LF              // LF - Line feed, new line.
	VT              // VT - Vertical tabulation.
	FF              // FF - Form feed, new page.
	CR              // CR - Carriage return.
	SO              // SO - Shift out.
	SI              // SI - Shift in.
	DLE             // DLE - Data Link Escape.
	DC1             // DC1 - Device control 1.
	DC2             // DC2 - Device control 2.
	DC3             // DC3 - Device control 3.
	DC4             // DC4 - Device control 4.
	NAK             // NAK - Negative acknowledge.
	SYN             // SYN - Synchronous idle.
	ETB             // ETB - End of transmission block.
	CAN             // CAN - Cancel.
	EM              // EM - End of medium.
	SUB             // SUB - Substitute.
	ESC             // ESC - Escape.
	FS              // FS - File separator.
	GS              // GS - Group separator.
	RS              // RS - Record separator.
	US              // US - Unit separator.
	SP              // SP - Space.
)
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

This section is empty.

Functions

func ImageToBin

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

func ImageToBit

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

func ImageToBytes added in v0.0.4

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

Logo returns the library logo.

func ResetGrayLevel added in v0.1.5

func ResetGrayLevel()

ResetGrayLevel resets the level of gray that should be visible when printing to default value.

func SetGrayLevel added in v0.1.5

func SetGrayLevel(l uint8)

SetGrayLevel sets the level of gray that should be visible when printing.

Types

type Cmd

type Cmd interface {
	// Sizing sets the number of characters per line (CPL) and pixels per line (PPL) after the command set has been initialized.
	Sizing(cpl, ppl int)

	// CPL returns the set number of characters per line.
	CPL() int

	// PPL returns the set number of pixel per line.
	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)

	// UpsideDown selects upside-down print mode on/off.
	UpsideDown(b bool)

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

	// ClockwiseRotation turns 90' clockwise rotation mode on/off.
	ClockwiseRotation(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)

	// Print performs final preparation of the document before printing.
	Print()
}

func NewEscape

func NewEscape(cpl, ppl int, w io.Writer, opts ...Options) Cmd

NewEscape returns the most popular set of printer commands for the given configuration.

This function creates a new escape sequence command set for printing images and text.

Parameters:

  • cpl: characters per line.
  • ppl: pixels per line.
  • w: the writer to which the commands will be sent.
  • opts: a variadic list of options to customize the behavior of the command set.

Options: You can customize various aspects of the postscript command set using the following options:

  • WithBarCodeFunc(barCodeFunc): sets a custom function for generating barcodes.
  • WithQRCodeFunc(qrCodeFunc): sets a custom function for generating QR codes.
  • WithImageFuncVersion(n): switches the image printing function, where:
  • n = 1: uses the [GS 8 L ... GS ( L] print image command.
  • n = 2: uses the [ESC * ! ... ESC J] print image command.

Note: By default, the obsolete [GS v ...] print image command is used.

Example Usage:

cmd := NewEscape(48, 576, writer, WithImageFuncVersion(2))

In this example, a new escape sequence command set is created with 48 characters per line, 576 pixels per line. The image printing function is set to use the [ESC * ! ... ESC J] command sequence (version 2).

func NewPostscript added in v0.1.0

func NewPostscript(cpl, ppl int, w io.Writer, opts ...Options) Cmd

NewPostscript returns the postscript set of printer commands configured with the specified parameters.

This function creates a new postscript command set for printing with customizable options.

Parameters:

  • cpl: characters per line.
  • ppl: pixels per line.
  • w: the writer to which the commands will be sent.
  • opts: a variadic list of options to customize the behavior of the command set.

Options: You can customize various aspects of the postscript command set using the following options:

  • WithBarCodeFunc(barCodeFunc): sets a function for generating barcodes.
  • WithQRCodeFunc(qrCodeFunc): sets a function for generating QR codes.
  • WithPageHeight(height): sets the page height to the specified value.

Example Usage:

cmd := NewPostscript(48, 576, writer, WithPageHeight(5670), WithBarCodeFunc(barCodeFunc), WithQRCodeFunc(qrCodeFunc))

In this example, a new postscript command set is created with 48 characters per line, 576 pixels per line. The page height is set to 5670 units, and functions for generating barcodes and QR codes are provided.

Default Initialization: If no options are specified, the postscript command set initializes with height: 400 units.

Note: If functions for generating barcodes and QR codes not provided, the call to print them will be skipped.

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, opts ...Options) Cmd

NewStar returns the star set of printer commands for the given configuration.

This function creates a new star sequence command set for printing images and text.

Parameters:

  • cpl: characters per line.
  • ppl: pixels per line.
  • w: the writer to which the commands will be sent.
  • opts: a variadic list of options to customize the behavior of the command set.

Options: You can customize various aspects of the postscript command set using the following options:

  • WithBarCodeFunc(barCodeFunc): sets a custom function for generating barcodes.
  • WithQRCodeFunc(qrCodeFunc): sets a custom function for generating QR codes.

Example Usage:

cmd := NewStar(48, 576, writer)

In this example, a new star sequence command set is created with 48 characters per line, 576 pixels per line.

type Options added in v0.1.1

type Options interface {
	// contains filtered or unexported methods
}

func WithBarCodeFunc added in v0.1.1

func WithBarCodeFunc(fn func(byte, string) image.Image) Options

func WithImageFuncVersion added in v0.1.1

func WithImageFuncVersion(v byte) Options

func WithPageHeight added in v0.1.5

func WithPageHeight(height float64) Options

func WithQRCodeFunc added in v0.1.1

func WithQRCodeFunc(fn func(string) image.Image) Options

Jump to

Keyboard shortcuts

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