Skip to content

@thermal-print/core

Core types and interfaces for thermal printing.

The @thermal-print/core package provides the universal intermediate representation (IR) that bridges renderers (React, Vue, etc.) and printer formats (ESC/POS, Star, ZPL, etc.).


PrintNode

The core data structure representing a thermal print document tree.

typescript
interface PrintNode {
  type: string;
  props: any;
  children: PrintNode[];
  style?: any;
}
PropertyTypeDescription
typestringElement type (document, page, view, text, image, textnode)
propsanyComponent props
childrenPrintNode[]Child nodes
styleanyStyle object (TextStyle or ViewStyle)

Example:

typescript
const printNode: PrintNode = {
  type: 'document',
  props: {},
  children: [
    {
      type: 'page',
      props: {},
      children: [
        {
          type: 'text',
          props: {},
          children: [],
          style: { fontSize: 24, fontWeight: 'bold', textAlign: 'center' }
        }
      ]
    }
  ]
};

ElementNode

Deprecated

ElementNode is deprecated. Use PrintNode instead. Will be removed in v1.0.

typescript
type ElementNode = PrintNode;

StandardElementType

Standard element types that all adapters must normalize to.

typescript
const StandardElementType = {
  DOCUMENT: 'document',
  PAGE: 'page',
  VIEW: 'view',
  TEXT: 'text',
  IMAGE: 'image',
  TEXTNODE: 'textnode',
} as const;
ConstantValueDescription
DOCUMENT'document'Root document container
PAGE'page'Page element (maps to receipt page)
VIEW'view'Container/layout element
TEXT'text'Text element
IMAGE'image'Image element
TEXTNODE'textnode'Raw text node (string children)

Example:

typescript
import { StandardElementType } from '@thermal-print/core';

if (node.type === StandardElementType.TEXT) {
  // Handle text node
}

TextStyle

Text styling properties that map to printer commands.

typescript
interface TextStyle {
  fontSize?: number;
  fontWeight?: string | number;
  fontFamily?: string;
  textAlign?: 'left' | 'center' | 'right';
  paddingLeft?: number;
  paddingRight?: number;
}

Properties

PropertyTypeESC/POSPDFDescription
fontSizenumberMapped to 4 sizesDirectFont size in pixels
fontWeightstring | numberBold detectionDirect'bold' or 700+ for bold
fontFamilystringBold onlyDirectOnly 'Helvetica-Bold' recognized
textAlign'left' | 'center' | 'right'YesYesText alignment
paddingLeftnumberIgnoredYesLeft padding (PDF only)
paddingRightnumberIgnoredYesRight padding (PDF only)

Font Size Mapping (ESC/POS)

Thermal printers only support 4 discrete character sizes:

Font SizeESC/POS SizeDescription
8-12px1x1Normal size
13-18px1x2Double height
19-24px2x1Double width
25+px2x2Double size (maximum)

Example:

typescript
const textStyle: TextStyle = {
  fontSize: 24,           // → 2x1 (double width)
  fontWeight: 'bold',     // → ESC E 1 (emphasized)
  textAlign: 'center'     // → ESC a 1
};

Bold Detection

Text is rendered as bold when any of these conditions are met:

  • fontWeight: 'bold'
  • fontWeight: 700 (or higher)
  • fontFamily: 'Helvetica-Bold'

ViewStyle

Container/layout styling properties.

typescript
interface ViewStyle {
  display?: string;
  flexDirection?: 'row' | 'column';
  justifyContent?: string;
  alignItems?: string;
  padding?: number;
  paddingTop?: number;
  paddingBottom?: number;
  paddingLeft?: number;
  paddingRight?: number;
  margin?: number;
  marginTop?: number;
  marginBottom?: number;
  marginLeft?: number;
  marginRight?: number;
  borderTop?: string;
  borderBottom?: string;
  width?: string | number;
  height?: string | number;
}

Layout Properties

PropertyTypeESC/POSDescription
flexDirection'row' | 'column'YesLayout direction (default: 'column')
justifyContentstringPartialMain axis alignment
alignItemsstringPartialCross axis alignment (text fallback only)
widthstring | numberYesColumn width ('50%' or character count)

flexDirection

ValueEffect
'column'Stacks children vertically (default)
'row'Side-by-side columns

justifyContent (row layouts only)

ValueESC/POS SupportEffect
'space-between'YesTwo-column layout with maximum gap
'center'YesCenters entire row
'flex-start'DefaultNo special handling
'flex-end'NoNot implemented
'space-around'NoNot implemented
'space-evenly'NoNot implemented

alignItems

Limited Support

Only used as text alignment fallback, NOT for vertical positioning.

ValueEffect
'center'Sets text alignment to center
'flex-end'Sets text alignment to right

Spacing Properties

Spacing is converted to line feeds: ~20 pixels = 1 line feed

PropertyTypeESC/POSPDFDescription
paddingnumberTop/bottom onlyYesShorthand (all sides for PDF)
paddingTopnumberYesYesTop padding
paddingBottomnumberYesYesBottom padding
paddingLeftnumberIgnoredYesLeft padding (PDF only)
paddingRightnumberIgnoredYesRight padding (PDF only)
marginnumberTop/bottom onlyYesShorthand
marginTopnumberYesYesTop margin
marginBottomnumberYesYesBottom margin
marginLeftnumberIgnoredYesLeft margin (PDF only)
marginRightnumberIgnoredYesRight margin (PDF only)

Example:

typescript
const viewStyle: ViewStyle = {
  paddingTop: 40,    // ~2 line feeds before content
  paddingBottom: 20  // ~1 line feed after content
};

Border Properties

PropertyTypeESC/POSDescription
borderTopstringYesTop divider line
borderBottomstringYesBottom divider line

Border Style Detection:

Style ContainsRenders As
'dashed'- characters
Any other characters (solid)

INFO

Borders always render full-width across the paper. Border width and color are ignored (thermal printers are monochrome).

Example:

typescript
const viewStyle: ViewStyle = {
  borderBottom: '1px solid black',  // Solid line: ────────────
  borderTop: '1px dashed gray'      // Dashed line: ------------
};

Size Properties

PropertyTypeESC/POSPDFDescription
widthstring | numberYesYesColumn width
heightstring | numberIgnoredYesFixed height (PDF only)

width

FormatExampleDescription
Percentage'50%'50% of paper width
Number20Fixed 20 characters
undefined-Auto-calculated

Deprecated Properties

PropertyStatus
displayExtracted but not implemented
marginLeftExtracted but ignored
marginRightExtracted but ignored

Thermal Printer Limitations

When designing for thermal printers, keep these constraints in mind:

Not Supported on Thermal Printers

FeatureReason
Left/right marginsHardware limitation
Left/right paddingHardware limitation
Horizontal centering of elementsUse textAlign instead
Variable fontsSingle built-in font
Font colorsMonochrome only
Fine-grained font sizesOnly 4 sizes (1x1, 1x2, 2x1, 2x2)
Fixed heightsPaper expands to fit content

Works Differently

FeatureESC/POS Behavior
Padding/marginTop/bottom only, converted to line feeds
BordersFull-width divider lines only
Font sizeMapped to 4 discrete sizes
BoldUses emphasis mode (ESC E)

Best Practices

  1. Use flexDirection: 'row' for multi-column layouts
  2. Use justifyContent: 'space-between' for receipt-style layouts (label + value)
  3. Use textAlign instead of alignItems for text alignment
  4. Use borders for visual separation (they render as full-width dividers)
  5. Test with actual printers - emulators may not show all limitations

Released under the MIT License.