npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@thermal-print/escpos

v0.3.0

Published

ESC/POS command generation and thermal printer control

Readme

@thermal-print/escpos

ESC/POS command generation and thermal printer control.

📦 Installation

pnpm add @thermal-print/escpos

🎯 Purpose

Converts PrintNode trees (universal IR) to ESC/POS command buffers for thermal printers.

Architecture:

PrintNode → ESCPOSGenerator → Buffer (ESC/POS commands)

🚀 Quick Start

import { printNodesToESCPOS } from "@thermal-print/escpos";
import { PrintNode } from "@thermal-print/core";

const printNode: PrintNode = {
  type: "document",
  props: {},
  children: [
    {
      type: "text",
      props: { children: "Hello World" },
      children: [],
      style: { textAlign: "center", fontSize: 20 },
    },
  ],
  style: {},
};

// Convert to ESC/POS buffer
const buffer = await printNodesToESCPOS(printNode, {
  paperWidth: 48,
  cut: "full",
});

// Send to printer
await printer.write(buffer);

📖 API

printNodesToESCPOS(printNode, options)

Main conversion function.

Parameters:

  • printNode: PrintNode - Root node of the tree to convert
  • options?: PrintNodeToESCPOSOptions - Conversion options

Options:

interface PrintNodeToESCPOSOptions {
  paperWidth?: number; // Characters per line (default: 48)
  encoding?: string; // Character encoding (default: 'utf-8')
  debug?: boolean; // Enable debug output
  cut?: boolean | "full" | "partial"; // Paper cut (default: 'full')
  feedBeforeCut?: number; // Lines to feed before cut (default: 3)
  commandAdapter?: "escpos" | "escbematech"; // Protocol (default: 'escpos')
}

Returns: Promise<Buffer> - ESC/POS command buffer

🎛 Command Adapters

ESC/POS (Default)

Standard ESC/POS protocol compatible with most thermal printers.

const buffer = await printNodesToESCPOS(printNode, {
  commandAdapter: "escpos",
});

ESC/Bematech

Bematech MP-4200 TH specific protocol.

const buffer = await printNodesToESCPOS(printNode, {
  commandAdapter: "escbematech",
});

🔧 Advanced Usage

Custom Command Adapter

import { CommandAdapter, ESCPOSGenerator } from "@thermal-print/escpos";

class CustomAdapter implements CommandAdapter {
  getName(): string {
    return "custom";
  }

  getInitCommand(): number[] {
    return [0x1b, 0x40]; // ESC @
  }

  // ... implement other methods
}

const buffer = await printNodesToESCPOS(printNode, {
  commandAdapter: new CustomAdapter(),
});

Direct Generator Usage

import { ESCPOSGenerator, TreeTraverser } from "@thermal-print/escpos";

const generator = new ESCPOSGenerator(48, "utf-8");
generator.initialize();

const traverser = new TreeTraverser(generator);
await traverser.traverse(printNode);

generator.cutFullWithFeed(3);
const buffer = generator.getBuffer();

🎨 Styling Support

Text Styles

  • fontSize: Maps to character sizes (1x1, 1x2, 2x1, 2x2)
  • fontWeight: 'bold' or numeric ≥700
  • textAlign: 'left', 'center', 'right'

Layout Styles

  • flexDirection: 'row' (side-by-side), 'column' (stacked)
  • justifyContent: 'space-between', 'center', etc.
  • padding/margin: Top and bottom spacing (converted to line feeds)
  • borderTop/borderBottom: Divider lines (solid or dashed)
  • width: Column width (percentage or fixed characters)

🌍 Character Encoding

CP860 (Default)

Brazilian Portuguese support with special characters: ç, á, é, í, ó, ú, ã, õ

const buffer = await printNodesToESCPOS(printNode, {
  encoding: "cp860",
});

📏 Paper Widths

Common thermal printer paper widths:

  • 58mm = 32 characters
  • 80mm = 48 characters (default)
  • 112mm = 64 characters
const buffer = await printNodesToESCPOS(printNode, {
  paperWidth: 48, // 80mm paper
});

🖼 Image Support

Images are automatically:

  • Resized to fit paper width
  • Converted to grayscale
  • Converted to monochrome (1-bit)
  • Printed using ESC/POS raster graphics

Requires jimp for image processing (optional dependency).

📄 License

MIT © Gabriel Martinusso