poster

module
v1.8.1-0...-dd5fdc1 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2026 License: MIT

README

Poster: Thermal Printer Driver & ESC/POS Utility

Go Version Platform License CI

CodeQL Release

codecov Go Report Card Go Reference

Dependabot Status Conventional Commits

Poster Logo

A professional, production-ready Go library and command-line utility for controlling ESC/POS thermal printers. Designed for high-reliability retail and POS environments, it features a robust JSON-based document protocol, native Windows Spooler integration, an advanced graphics engine, and a visual emulator for testing without physical hardware.

🚀 Key Features

  • JSON Document Protocol: Define print jobs using a clean, versioned JSON schema (v1.0). Decouples business logic from hardware commands with full schema validation.
  • Native Windows Integration: Prints directly via the Windows Print Spooler API (winspool.drv), supporting USB, Serial, and Network printers installed in Windows.
  • Advanced Graphics Engine:
    • High-quality image printing with Atkinson Dithering.
    • Automatic scaling with bilinear interpolation.
    • Supports PNG, JPG, BMP formats.
  • Smart QR & Barcodes: Automatically chooses between native printer firmware commands (fastest) or software rendering (maximum compatibility) based on the printer profile.
  • Dynamic Table Layout: Built-in engine for generating perfectly aligned receipts with word wrapping, multi-column support, configurable spacing, automatic overflow detection, and smart column auto-reduction that preserves small columns while shrinking larger ones to fit paper width.
  • Visual Emulator: Render print jobs as PNG images for preview and testing without physical hardware.
  • Hardware Agnostic: Includes profiles for standard 80mm (Epson-compatible), 58mm (generic), PT-210, GP-58N, and EC-PM-80250 printers.
  • Smart Printer Discovery: Auto-detects thermal/POS printers from installed Windows devices, filtering virtual printers and matching common hardware patterns.
  • 11 Command Types: text, image, barcode, qr, table, separator, feed, cut, raw, pulse, beep.
  • Raw Command Support: Send raw ESC/POS bytes when full control is needed.

🏗️ Architecture

The project follows a layered architecture to ensure modularity and testability:

graph TD
    JSON["JSON Document<br/>(v1.0 Schema)"] --> Parser["Parser & Validator<br/>(pkg/document/schema)"]
    Parser --> Builder["Command Builder<br/>(pkg/document/builder)"]
    Builder --> Executor["Command Executor<br/>(pkg/document/executor)"]
    Executor --> Service["Printer Service<br/>(pkg/service)"]

    subgraph "Core Logic"
        Service --> Protocol["ESC/POS Composer<br/>(pkg/composer)"]
        Service --> Profile["Device Profile<br/>(pkg/profile)"]
        Service --> Graphics["Graphics Engine<br/>(pkg/graphics)"]
        Service --> Tables["Table Engine<br/>(pkg/tables)"]
    end

    subgraph "Command Modules (pkg/commands)"
        Protocol --> Barcode["Barcode"]
        Protocol --> BitImage["Bit Image"]
        Protocol --> Character["Character"]
        Protocol --> LineSpacing["Line Spacing"]
        Protocol --> MechControl["Mechanism Control"]
        Protocol --> PrintCmd["Print"]
        Protocol --> PrintPos["Print Position"]
        Protocol --> QRCode["QR Code"]
    end

    Service --> Connector["Connection Interface<br/>(pkg/connection)"]

    subgraph "Output Layer"
        Connector --> WinAPI["Windows Spooler API<br/>(winspool.drv)"]
        Connector --> Network["Network<br/>(TCP/9100)"]
        Connector --> Serial["Serial<br/>(COM Port)"]
        Connector --> FileOut["File<br/>(Debug Output)"]
        Connector --> Emulator["Visual Emulator<br/>(pkg/emulator)"]
        WinAPI --> Device["Physical Printer"]
        Emulator --> PNG["PNG Image"]
    end
Package Structure
Package Description
pkg/commands ESC/POS command implementations (barcode, bitimage, character, linespacing, mechanismcontrol, print, printposition, qrcode)
pkg/composer ESC/POS byte sequence generation and protocol composition
pkg/connection Connection interfaces — Windows Spooler, Network, Serial, File
pkg/constants Shared constants, unit conversions, and default values
pkg/document Document parsing, building, and execution (schema, builder, executor)
pkg/emulator Visual emulator for rendering print jobs as PNG images
pkg/graphics Image processing pipeline — dithering, scaling, bitmap handling
pkg/profile Printer profiles and character encoding tables
pkg/service High-level printer service facade (PrinterActions interface)
pkg/tables Table engine — column alignment, word wrapping, header styling, auto-reduction

📦 Installation

Prerequisites
  • Go 1.24 or higher
  • Windows OS (for native spooler support; cross-platform for library/emulator use)
As a Go Module
go get github.com/adcondev/poster@latest
Build from Source
# Clone the repository
git clone https://github.com/adcondev/poster.git
cd poster

# Build the binary
go build -o poster.exe ./cmd/poster

# Or use Taskfile
task build

📖 Usage

Command Line Interface (CLI)

The poster utility takes a JSON document and sends it to a specified printer.

# Print a document to a specific printer
poster.exe -file receipt.json -printer "EPSON TM-T88V"

# Dry-run (validate JSON without printing)
poster.exe -file receipt.json --dry-run

# List all installed printers (Windows only)
poster.exe --list

# List thermal/POS printers only
poster.exe --list-thermal

# List physical printers only (exclude virtual)
poster.exe --list-physical

# Output printer list as JSON
poster.exe --list --json

# Show version
poster.exe -v
JSON Document Example

Create a file named ticket.json:

{
  "version": "1.0",
  "profile": {
    "model": "Generic 80mm",
    "paper_width": 80,
    "dpi": 203,
    "has_qr": true
  },
  "commands": [
    {
      "type": "text",
      "data": {
        "content": {
          "text": "STORE NAME",
          "content_style": {
            "bold": true,
            "size": "2x2",
            "align": "center"
          }
        }
      }
    },
    {
      "type": "separator",
      "data": {
        "char": "-"
      }
    },
    {
      "type": "table",
      "data": {
        "definition": {
          "columns": [
            { "header": "Item", "width": 20, "align":  "left" },
            { "header":  "Price", "width": 10, "align": "right" }
          ]
        },
        "show_headers": true,
        "rows": [
          ["Coffee", "$3.50"],
          ["Sandwich", "$8.00"]
        ],
        "options": {
          "header_bold": true,
          "word_wrap": true,
          "column_spacing": 1,
          "auto_reduce": true
        }
      }
    },
    {
      "type": "qr",
      "data": {
        "data": "https://example.com",
        "align": "center",
        "pixel_width": 150
      }
    },
    {
      "type": "cut",
      "data": {
        "feed": 3
      }
    }
  ]
}
Library Usage (Go)

You can also use the packages directly in your Go application:

package main

import (
	"github.com/adcondev/poster/pkg/composer"
	"github.com/adcondev/poster/pkg/connection"
	"github.com/adcondev/poster/pkg/profile"
	"github.com/adcondev/poster/pkg/service"
)

func main() {
	// 1. Configure Profile
	prof := profile.CreateProfile80mm()

	// 2. Connect to Printer
	conn, _ := connection.NewWindowsPrintConnector("POS-80")
	defer conn.Close()

	// 3. Initialize Service
	proto := composer.NewEscpos()
	printer, _ := service.NewPrinter(proto, prof, conn)
	defer printer.Close()

	// 4. Print
	printer.Initialize()
	printer.PrintLine("Hello World!")
	printer.FullFeedAndCut(2)
}

📋 Supported Commands

Command Description
text Print formatted text with styles (bold, underline, inverse, sizing)
image Print images with dithering and scaling options
barcode Generate barcodes (CODE128, EAN13, UPC-A, CODE39, etc.)
qr Generate QR codes with optional logos and human-readable text
table Create formatted tables with column alignment and word wrapping
separator Print separator lines
feed Advance paper by specified lines
cut Perform full or partial paper cut
raw Send raw ESC/POS bytes directly
pulse Send electrical pulse to open cash drawer
beep Emit buzzer alert sound

For complete documentation, see api/v1/DOCUMENT_V1.md.

⚙️ Configuration

Connection Types
Type Status Description
windows ✅ Implemented Windows Print Spooler (default). USB/Network printers installed in Windows.
network 🔜 Planned Direct network connection via Raw TCP/9100
serial 🔜 Planned Serial/USB direct connection (COM ports)
file 🔜 Planned Output to file for debugging or emulator testing
Printer Profiles

The library includes built-in profiles for common hardware:

Profile Description
CreateProfile80mm() Standard ESC/POS 80mm (Epson TM-T88, etc.)
CreateProfile58mm() Generic 58mm thermal printers
CreatePt210() PT-210 portable printer with QR support
CreateGP58N() GP-58N 58mm thermal printer
CreateECPM80250() EC-PM-80250 80mm thermal printer

🎨 Visual Emulator

The pkg/emulator package provides a visual emulator that renders print jobs as PNG images:

import (
	"github.com/adcondev/poster/pkg/emulator"
)

// Create emulator with configuration
emu, _ := emulator.NewEngine(emulator.DefaultConfig())

// Render text
emu.SetBold(true)
emu.SetSize(2, 2)
emu.AlignCenter()
emu.PrintLine("STORE NAME")

// Render and save as PNG
img := emu.Render()

📊 Code Coverage

📈 Coverage Trends

Coverage Graph

🤝 Contributing

Contributions are welcome! Please read CONTRIBUTING.md for guidelines on how to submit pull requests. This project uses Conventional Commits and enforces semantic PR titles.

📄 License

This project is licensed under the MIT License — see the LICENSE file for details.

Directories

Path Synopsis
cmd
poster command
Package main es el ejecutor principal de la librería poster Uso: poster [options] <json_file> [printer_name]
Package main es el ejecutor principal de la librería poster Uso: poster [options] <json_file> [printer_name]
examples
beeper command
Package main demuestra cómo enviar comandos RAW no estándar (específicos del fabricante).
Package main demuestra cómo enviar comandos RAW no estándar (específicos del fabricante).
builder/extended command
Package main demonstrates the fluent API for building print documents
Package main demonstrates the fluent API for building print documents
builder/minimal command
Package main implements a minimal example of using the document builder
Package main implements a minimal example of using the document builder
document/basic command
Package main implements an example of printing a document using the poster library in JSON print job.
Package main implements an example of printing a document using the poster library in JSON print job.
document/qrcode command
Package main implements an example of printing a document using the poster library in JSON print job.
Package main implements an example of printing a document using the poster library in JSON print job.
document/tables command
Package main is an example of printing a document with tables using the poster library in JSON format.
Package main is an example of printing a document with tables using the poster library in JSON format.
emulator/basic command
Package main in an example demonstrating the ESC/POS emulator functionality
Package main in an example demonstrating the ESC/POS emulator functionality
emulator/image command
Package main demonstrates image embedding in the ESC/POS emulator
Package main demonstrates image embedding in the ESC/POS emulator
graphics/base64 command
Package main demonstrates the new advanced graphics engine for ESC/POS printing
Package main demonstrates the new advanced graphics engine for ESC/POS printing
internal
calculate
Package calculate proporciona funciones para cálculos relacionados con la impresión.
Package calculate proporciona funciones para cálculos relacionados con la impresión.
load
Package load provides utility functions for image and file loading and decoding.
Package load provides utility functions for image and file loading and decoding.
testutils
Package testutils provides utility functions for testing byte slices and error handling.
Package testutils provides utility functions for testing byte slices and error handling.
pkg
commands/barcode
Package barcode implements ESC/POS commands for barcode printing functionality.
Package barcode implements ESC/POS commands for barcode printing functionality.
commands/bitimage
Package bitimage implements ESC/POS commands for bit image functionality.
Package bitimage implements ESC/POS commands for bit image functionality.
commands/character
Package character implements ESC/POS commands for character formatting and appearance.
Package character implements ESC/POS commands for character formatting and appearance.
commands/linespacing
Package linespacing implements ESC/POS commands for line spacing control.
Package linespacing implements ESC/POS commands for line spacing control.
commands/mechanismcontrol
Package mechanismcontrol implements ESC/POS commands for mechanism control functionality.
Package mechanismcontrol implements ESC/POS commands for mechanism control functionality.
commands/print
Package print implements ESC/POS commands for basic printing operations.
Package print implements ESC/POS commands for basic printing operations.
commands/printposition
Package printposition implements ESC/POS commands for print position control.
Package printposition implements ESC/POS commands for print position control.
commands/qrcode
Package qrcode implements ESC/POS commands for QR Code generation and printing.
Package qrcode implements ESC/POS commands for QR Code generation and printing.
commands/shared
Package shared contains types and functions shared across multiple ESC/POS command packages.
Package shared contains types and functions shared across multiple ESC/POS command packages.
composer
Package composer provides a high-level interface for composing ESC/POS commands for POS printers by combining various capabilities.
Package composer provides a high-level interface for composing ESC/POS commands for POS printers by combining various capabilities.
connection
Package connection provides platform-specific printer connection implementations for ESC/POS printers.
Package connection provides platform-specific printer connection implementations for ESC/POS printers.
constants
Package constants provides centralized default values for the entire library.
Package constants provides centralized default values for the entire library.
document/builder
Package builder provides a fluent API for creating print documents.
Package builder provides a fluent API for creating print documents.
document/executor
Package executor parses and executes print documents on ESC/POS printers.
Package executor parses and executes print documents on ESC/POS printers.
document/schema
Package schema contiene las definiciones de estructuras para documentos de impresión
Package schema contiene las definiciones de estructuras para documentos de impresión
emulator
Package emulator provides a high-fidelity thermal printer emulation engine for Go.
Package emulator provides a high-fidelity thermal printer emulation engine for Go.
emulator/fonts
Package fonts provides embedded font resources for the emulator.
Package fonts provides embedded font resources for the emulator.
graphics
Package graphics provides utilities for handling monochrome bitmaps for ESC/POS printers
Package graphics provides utilities for handling monochrome bitmaps for ESC/POS printers
profile
Package profile define las características físicas y capacidades de las impresoras térmicas.
Package profile define las características físicas y capacidades de las impresoras térmicas.
service
Package service provides implementations for various POS printer service.
Package service provides implementations for various POS printer service.
tables
Package tables provides table generation and rendering for ESC/POS thermal printers.
Package tables provides table generation and rendering for ESC/POS thermal printers.

Jump to

Keyboard shortcuts

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