poster

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

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

Go to latest
Published: Feb 23, 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

POS Printer 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), and PT-210 portable printers.
  • 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] --> Parser[Parser & Validator]
    Parser --> Executor[Command Executor]
    Executor --> Service[Printer Service]

    subgraph Core Logic
        Service --> Protocol[ESC/POS Composer]
        Service --> Profile[Device Profile]
        Service --> Graphics[Graphics Engine]
        Service --> Tables[Table Engine]
    end

    Service --> Connector[Connection Interface]

    subgraph Output Layer
        Connector --> WinAPI[Windows Spooler API]
        Connector --> Emulator[Visual Emulator]
        WinAPI --> Device[Physical Printer]
        Emulator --> PNG[PNG Image]
    end
Package Structure
Package Description
pkg/commands ESC/POS command implementations (barcode, character, qrcode, etc.)
pkg/composer ESC/POS byte sequence generation
pkg/connection Connection interfaces (Windows Spooler, Network, Serial, File)
pkg/constants Shared constants and unit conversions
pkg/document Document parsing, building, and execution (schema, builder, executor)
pkg/emulator Visual emulator for rendering print jobs as images
pkg/graphics Image processing, dithering, and bitmap handling
pkg/profile Printer profiles and character encoding tables
pkg/service High-level printer service facade
pkg/table Create formatted tables with column alignment, word wrapping, header styling, and automatic width reduction for overflow protection

📦 Installation

Prerequisites
  • Go 1.24 or higher
  • Windows OS (for native spooler support)
Build from Source
# Clone the repository
git clone https://github.com/adcondev/poster.git

# Navigate to the project directory
cd poster

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

📖 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"

# Print via network
poster.exe -t network -network 192.168.1.100:9100 receipt.json

# Print via serial port
poster.exe -t serial -serial COM1 -baud 115200 receipt.json

# Output to file (for debugging or emulator)
poster.exe -t file -output receipt.prn receipt.json

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

# Show version
poster.exe -v

# List available options
poster.exe -h
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

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

⚙️ Configuration

Connection Types
Type Description
windows Windows Print Spooler (default). Best for USB/Network printers installed in Windows.
network Direct network connection via Raw TCP/9100
serial Serial/USB direct connection (COM ports)
file 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 specific tweaks

🎨 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.Config{
	PaperWidth: 80,
	DPI:        203,
})

// Render document and save as PNG
img := emu.Render(document)
emu.SaveImage("receipt_preview.png")

📊 Code Coverage

📈 Coverage Trends

Coverage Graph

🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

📄 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