Skip to content

API Reference

Complete API reference for all @thermal-print packages.

@thermal-print/react

Components

Document

Root wrapper for thermal printer documents.

tsx
<Document>
  <Page>...</Page>
</Document>

Props:

PropTypeDescription
childrenReactNodePage components

Page

Semantic page wrapper for content organization.

tsx
<Page size={{ width: 227, height: 300 }} style={{ padding: 10 }}>
  <Text>Content</Text>
</Page>

Props:

PropTypeDefaultDescription
childrenReactNode-Page content
styleViewStyle-Page styling
size{ width: number; height?: number }-Page dimensions in points (for PDF)
wrapbooleanundefinedEnable dynamic height (for PDF)

View

Layout container with flexbox support.

tsx
// Column layout (default)
<View style={{ padding: 10 }}>
  <Text>Item 1</Text>
  <Text>Item 2</Text>
</View>

// Row layout
<View style={{ flexDirection: 'row', justifyContent: 'space-between' }}>
  <Text>Left</Text>
  <Text>Right</Text>
</View>

// Divider
<View style={{ borderBottom: '1px solid black' }} />

Props:

PropTypeDescription
childrenReactNodeView content
styleViewStyleView styling

Text

Text content with styling.

tsx
<Text style={{ fontSize: 20, textAlign: 'center', fontWeight: 'bold' }}>
  Hello World
</Text>

Props:

PropTypeDescription
childrenstringText content
styleTextStyleText styling

Image

Images converted to monochrome for thermal printing.

tsx
<Image
  src="data:image/png;base64,..."
  style={{ textAlign: 'center' }}
/>

Props:

PropTypeDescription
srcstringImage source (URL or base64 data URI)
styleImageStyleImage styling

Preview

Browser preview component for development and testing.

tsx
<Preview paperWidth={48} showRuler scale={1.5}>
  <Document>
    <Page>
      <Text>Preview content</Text>
    </Page>
  </Document>
</Preview>

Props:

PropTypeDefaultDescription
childrenReactNode-Document to preview
idstring-Container ID (for PDF conversion)
paperWidthnumber48Paper width in characters
showRulerbooleanfalseShow character ruler
scalenumber1Scale factor for preview
styleCSSProperties-Additional CSS styling

StyleSheet

Utility for organizing styles (pass-through function).

tsx
const styles = StyleSheet.create({
  header: { fontSize: 20, fontWeight: 'bold' },
  text: { fontSize: 12 },
  row: { flexDirection: 'row', justifyContent: 'space-between' },
});

<Text style={styles.header}>Title</Text>

Font

Reserved for future use. Currently a no-op for thermal printers.

tsx
Font.register({
  family: 'Roboto',
  fonts: [{ src: 'https://...' }],
});

Functions

convertToESCPOS()

Converts React component directly to ESC/POS buffer.

tsx
import { convertToESCPOS } from '@thermal-print/react';

const buffer = await convertToESCPOS(<Receipt />, {
  paperWidth: 48,
  cut: 'full',
  feedBeforeCut: 3,
  encoding: 'utf-8',
  commandAdapter: 'escpos',
});

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

Parameters:

ParameterTypeDescription
componentReactElementReact component to convert
optionsConversionOptionsConversion options (optional)

Options:

OptionTypeDefaultDescription
paperWidthnumber48Characters per line
encodingstring'utf-8'Character encoding
debugbooleanfalseEnable debug output
cutboolean | 'full' | 'partial''full'Paper cut after printing
feedBeforeCutnumber3Lines to feed before cut
commandAdapter'escpos' | 'escbematech' | CommandAdapter'escpos'Protocol adapter
adapterComponentMapping | RendererAdapter-Custom component mapping

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


convertToPrintNodes()

Converts React component to PrintNode intermediate representation.

tsx
import { convertToPrintNodes } from '@thermal-print/react';

const printNode = convertToPrintNodes(<Receipt />);

// Manipulate the tree
printNode.children.push({
  type: 'text',
  props: { children: 'COPY' },
  children: [],
  style: { textAlign: 'center' }
});

Parameters:

ParameterTypeDescription
componentReactElementReact component to convert
adapterRendererAdapterCustom adapter (optional)

Returns: PrintNode | null - PrintNode tree


convertToHTML()

Converts React component to HTML/DOM for browser-based workflows.

tsx
import { convertToHTML } from '@thermal-print/react';

const result = await convertToHTML(<Receipt />, {
  containerId: 'thermal-receipt',
  keepInDOM: true,
  width: 400,
  format: 'element',
});

// Use the result
const element = result.content as HTMLElement;

// Cleanup when done
result.cleanup();

Parameters:

ParameterTypeDescription
componentReactElementReact component to convert
optionsConvertToHTMLOptionsConversion options (optional)

Options:

OptionTypeDefaultDescription
widthnumber400Container width in pixels
applyThermalStylesbooleantrueApply thermal styling
format'html' | 'element''element'Return format
containerIdstringauto-generatedCustom container ID
keepInDOMbooleanfalseKeep in DOM after render

Returns: Promise<ConvertToHTMLResult>

tsx
interface ConvertToHTMLResult {
  content: string | HTMLElement;  // Rendered content
  container: HTMLElement;         // Container element
  containerId: string;            // Container ID
  cleanup: () => void;            // Cleanup function
}

createAdapter()

Creates a custom component adapter for mapping component names.

tsx
import { createAdapter, convertToESCPOS } from '@thermal-print/react';

const adapter = createAdapter({
  Receipt: 'document',
  Section: 'page',
  Row: 'view',
  Label: 'text',
  Logo: 'image'
});

const buffer = await convertToESCPOS(<Receipt />, { adapter });

Parameters:

ParameterTypeDescription
mappingComponentMappingObject mapping component names to standard types

Returns: RendererAdapter


Types

ViewStyle

tsx
interface ViewStyle {
  flexDirection?: 'row' | 'column';
  justifyContent?: 'flex-start' | 'center' | 'flex-end' | 'space-between';
  alignItems?: 'flex-start' | 'center' | 'flex-end';
  padding?: number;
  paddingTop?: number;
  paddingBottom?: number;
  paddingLeft?: number;   // Not supported in ESC/POS
  paddingRight?: number;  // Not supported in ESC/POS
  margin?: number;
  marginTop?: number;
  marginBottom?: number;
  marginLeft?: number;    // Not supported in ESC/POS
  marginRight?: number;   // Not supported in ESC/POS
  borderTop?: string;     // e.g., '1px solid black'
  borderBottom?: string;  // e.g., '1px dashed black'
  width?: number | string; // For columns (e.g., '50%' or 24)
}

TextStyle

tsx
interface TextStyle {
  fontSize?: number;                        // Maps to ESC/POS sizes
  fontWeight?: 'normal' | 'bold' | number;  // 700+ = bold
  textAlign?: 'left' | 'center' | 'right';
}

PrintNode

tsx
interface PrintNode {
  type: string;           // 'document', 'page', 'view', 'text', 'image', 'textnode'
  props: any;             // Component props
  children: PrintNode[];  // Child nodes
  style?: any;            // Computed styles
}

@thermal-print/escpos

Functions

printNodesToESCPOS()

Converts PrintNode tree to ESC/POS buffer.

tsx
import { printNodesToESCPOS } from '@thermal-print/escpos';

const buffer = await printNodesToESCPOS(printNode, {
  paperWidth: 48,
  encoding: 'utf-8',
  cut: 'full',
  feedBeforeCut: 3,
  commandAdapter: 'escpos',
});

Parameters:

ParameterTypeDescription
printNodePrintNodeRoot PrintNode of the tree
optionsPrintNodeToESCPOSOptionsConversion options (optional)

Options:

OptionTypeDefaultDescription
paperWidthnumber48Width in characters
encodingstring'utf-8'Character encoding
debugbooleanfalseEnable debug output
cutboolean | 'full' | 'partial''full'Cut paper after printing
feedBeforeCutnumber3Lines to feed before cutting
commandAdapter'escpos' | 'escbematech' | CommandAdapter'escpos'Protocol adapter

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


Classes

ESCPOSGenerator

Low-level ESC/POS command generator for advanced usage.

tsx
import { ESCPOSGenerator } from '@thermal-print/escpos';

const generator = new ESCPOSGenerator(48, 'utf-8', false);

generator.initialize();
generator.setAlign('center');
generator.setBold(true);
generator.setCharacterSize(2, 2);
generator.addText('HELLO WORLD');
generator.addNewline();
generator.cutFullWithFeed(3);

const buffer = generator.getBuffer();

Constructor:

ParameterTypeDefaultDescription
paperWidthnumber48Paper width in characters
encodingstring'utf-8'Character encoding
debugbooleanfalseEnable debug output
commandAdapterCommandAdapterESCPOSCommandAdapterProtocol adapter

Methods:

MethodDescription
initialize()Initialize printer
setAlign(align)Set text alignment ('left', 'center', 'right')
setBold(enabled)Enable/disable bold
setCharacterSize(width, height)Set character size (1-2)
addText(text)Add text content
addNewline()Add line feed
addLineFeed(lines)Add multiple line feeds
cutFullWithFeed(lines)Full cut with line feed
cutPartialWithFeed(lines)Partial cut with line feed
addImage(imageData, width, height)Add raster image
getBuffer()Get final ESC/POS buffer

TreeTraverser

Traverses PrintNode tree and generates ESC/POS commands.

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

const generator = new ESCPOSGenerator(48);
const traverser = new TreeTraverser(generator);

await traverser.traverse(printNode);
const buffer = generator.getBuffer();

Command Adapters

ESCPOSCommandAdapter

Standard ESC/POS protocol adapter (default).

tsx
import { ESCPOSCommandAdapter } from '@thermal-print/escpos';

const adapter = new ESCPOSCommandAdapter();

ESCBematechCommandAdapter

Bematech-specific protocol adapter.

tsx
import { ESCBematechCommandAdapter } from '@thermal-print/escpos';

const adapter = new ESCBematechCommandAdapter();

Style Utilities

tsx
import {
  extractTextStyle,
  extractViewStyle,
  isBold,
  mapFontSizeToESCPOS,
  mapTextAlign,
  isDashedBorder,
  generateDividerLine,
  parseWidth,
  wrapText,
} from '@thermal-print/escpos';
FunctionDescription
extractTextStyle(props)Extract TextStyle from props
extractViewStyle(props)Extract ViewStyle from props
isBold(style)Check if style is bold
mapFontSizeToESCPOS(fontSize)Map font size to ESC/POS size
mapTextAlign(align)Map text alignment to ESC/POS
isDashedBorder(border)Check if border is dashed
generateDividerLine(width, dashed)Generate divider line string
parseWidth(width, totalWidth)Parse width to characters
wrapText(text, maxWidth)Wrap text to fit width

Encodings

tsx
import { encodeCP860 } from '@thermal-print/escpos';

const encoded = encodeCP860('Olá Mundo'); // Portuguese encoding

@thermal-print/pdf

Functions

printNodesToPDF()

Converts PrintNode tree to PDF using vector rendering.

tsx
import { printNodesToPDF } from '@thermal-print/pdf';

const pdfBytes = await printNodesToPDF(printNode, {
  paperWidth: 227,    // 80mm in points
  paperHeight: 'auto', // Dynamic height
  debug: false,
});

// Save or send PDF
fs.writeFileSync('receipt.pdf', pdfBytes);

Parameters:

ParameterTypeDescription
printNodePrintNodeRoot PrintNode of the tree
optionsPrintNodesToPDFOptionsPDF options (optional)

Options:

OptionTypeDefaultDescription
paperWidthnumber227Paper width in points (80mm)
paperHeightnumber | 'auto''auto'Paper height in points or dynamic
debugbooleanfalseEnable debug output

Returns: Promise<Uint8Array> - PDF bytes


convertToPDF()

Converts DOM element to PDF (browser-based).

tsx
import { convertToPDF } from '@thermal-print/pdf';

const result = await convertToPDF('receipt-container', {
  paperSize: '80mm',
  scale: 2,
  filename: 'receipt.pdf', // Auto-download
});

// Or open in new tab
window.open(result.url);

// Cleanup
result.cleanup();

Parameters:

ParameterTypeDescription
containerIdstringDOM element ID to convert
optionsConvertToPDFOptionsPDF options (optional)

Options:

OptionTypeDefaultDescription
paperSize'58mm' | '80mm' | '112mm''80mm'Paper size preset
scalenumber2Scale factor for quality
filenamestring-Filename for auto-download

Returns: Promise<ConvertToPDFResult>

tsx
interface ConvertToPDFResult {
  url: string;          // Blob URL of PDF
  blob: Blob;           // PDF blob
  cleanup: () => void;  // Revoke blob URL
}

Classes

PDFGenerator

Low-level PDF generation using jsPDF.

tsx
import { PDFGenerator } from '@thermal-print/pdf';

const pdf = new PDFGenerator({
  paperWidth: 227,
  paperHeight: 500,
});

pdf.addText('Hello World', { x: 10, y: 20, fontSize: 12 });
pdf.addLine(10, 30, 200, 30);

const bytes = pdf.getOutput();

@thermal-print/core

Types

PrintNode

Universal intermediate representation for printer documents.

tsx
interface PrintNode {
  type: string;           // Element type
  props: any;             // Component props
  children: PrintNode[];  // Child nodes
  style?: any;            // Computed styles
}

StandardElementType

Standard element type constants.

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

const types = {
  DOCUMENT: 'document',
  PAGE: 'page',
  VIEW: 'view',
  TEXT: 'text',
  IMAGE: 'image',
  TEXTNODE: 'textnode',
};

TextStyle

tsx
interface TextStyle {
  fontSize?: number;
  fontWeight?: 'normal' | 'bold' | number;
  textAlign?: 'left' | 'center' | 'right';
}

ViewStyle

tsx
interface ViewStyle {
  flexDirection?: 'row' | 'column';
  justifyContent?: 'flex-start' | 'center' | 'flex-end' | 'space-between';
  alignItems?: 'flex-start' | 'center' | 'flex-end';
  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?: number | string;
}

Constants

Paper Width Reference

Paper SizeWidth (mm)Width (points)Characters
58mm5816432
80mm8022748
112mm11231864

Font Size Mapping

fontSize (px)ESC/POS SizeDescription
8-121x1Normal
13-181x2Double height
19-242x1Double width
25+2x2Double both (max)

ESC/POS Commands Reference

CommandBytesDescription
Initialize0x1B 0x40Reset printer
Align Left0x1B 0x61 0x00Left alignment
Align Center0x1B 0x61 0x01Center alignment
Align Right0x1B 0x61 0x02Right alignment
Bold On0x1B 0x45 0x01Enable bold
Bold Off0x1B 0x45 0x00Disable bold
Full Cut0x1B 0x69Full paper cut
Partial Cut0x1B 0x6DPartial paper cut
Line Feed0x0ANew line

Released under the MIT License.