node-thermal-printer

repository·master·Indexed 21 days ago

https://github.com/klemen1337/node-thermal-printer

A Node.js module for controlling thermal printers from Epson, Star, Tanca, Daruma, Brother, and Custom brands. It supports printing text, tables, 1D barcodes, QR codes, and images via command line, network (TCP/IP), or system printer drivers. Features include text formatting, alignment, hardware controls for cutting and cash drawers, and support for various international character sets.

Tokens
9.9K
Snippets
43
Records
52
Agent score
74%

What's inside node-thermal-printer

  1. Configure node-thermal-printer for Browser environments

    master

    When using this module in a browser, the network interface will not work because browsers do not allow opening TCP sockets. You must instruct your bundler (e.g., Webpack) to ignore Node.js core dependencies like fs and net.

    For Webpack, use the IgnorePlugin:

    new webpack.IgnorePlugin({
      resourceRegExp: /^fs$|^net$/,
    }),
  2. Use System Printer Drivers

    master

    When using a system printer (via printer:auto or printer:Name), you must provide a driver. You can use the printer or electron-printer modules depending on your environment.

    Using Electron or Node.js drivers:

    const ThermalPrinter = require("node-thermal-printer").printer;
    const PrinterTypes = require("node-thermal-printer").types;
    const electron = typeof process !== 'undefined' && process.versions && !!process.versions.electron;
    
    let printer = new ThermalPrinter({
      type: PrinterTypes.EPSON,
      interface: 'printer:My Printer',
      driver: require(electron ? 'electron-printer' : 'printer')
    });

    Using a custom driver: You can pass a custom driver object during initialization or via setPrinterDriver():

    let printer = new ThermalPrinter({
      type: PrinterTypes.EPSON,
      interface: 'printer:My Printer',
      driver: MyCustomDriver
    });
    
    // Or after initialization:
    printer.setPrinterDriver(MyCustomDriver);
  3. Install node-thermal-printer

    master

    Install the module using npm:

    npm install node-thermal-printer

    Linux Requirements

    Linux users must install build-essential to support the module:

    sudo apt-get install build-essential
  4. Print 1D Barcodes (Epson)

    master

    Use the printBarcode method to print 1D barcodes on Epson-compatible printers. You must provide the barcode data, the specific Epson barcode type ID, and an optional settings object.

    Settings Options:

    • hriPos: Human readable character position (0 - 3: none, top, bottom, both).
    • hriFont: Human readable character font.
    • width: Barcode width.
    • height: Barcode height.
    var data = "GS1-128"     // Barcode data (string or buffer)
    var type = 74            // Barcode type (See Reference)
    var settings = {         // Optional Settings
      hriPos: 0,             // Human readable character 0 - 3 (none, top, bottom, both)
      hriFont: 0,            // Human readable character font
      width: 3,              // Barcode width
      height: 168            // Barcode height
    }
    
    printer.printBarcode(data, type, settings);
  5. Use ThermalPrinter methods for printing content

    master

    The ThermalPrinter instance provides a variety of methods to build a print job. Commands are appended to an internal buffer and executed when .execute() is called.

    Execution & Connection:

    • await printer.isPrinterConnected(): Returns boolean status.
    • await printer.execute(): Executes all queued commands. Returns success or throws error.
    • await printer.raw(Buffer): Prints raw data immediately.

    Text & Formatting:

    • printer.print(text) / printer.println(text): Append text (with or without newline).
    • printer.bold(bool): Set text bold.
    • printer.underline(bool) / printer.underlineThick(bool): Set underline thickness.
    • printer.alignCenter() / printer.alignLeft() / printer.alignRight(): Text alignment.
    • printer.setTextSize(height, width): Set text size (0-7).
    • printer.setTypeFontA() / printer.setTypeFontB(): Set font type.

    Layout & Lines:

    • printer.drawLine(): Draws a line.
    • printer.newLine(): Inserts a break line.
    • printer.leftRight(leftText, rightText): Prints text on left and right sides.
    • printer.table(array): Prints a table with equal columns.
    • printer.tableCustom(array): Prints a table with custom settings (text, align, width, cols, bold).

    Hardware Actions:

    • printer.openCashDrawer(): Kicks the cash drawer.
    • printer.cut() / printer.partialCut(): Cuts the paper.
    • printer.beep(): Sounds the internal buzzer.
    • printer.upsideDown(bool): Rotates content 180 degrees.
  6. Initialize the ThermalPrinter class

    master

    To use the printer, instantiate the ThermalPrinter class with a configuration object.

    Key Configuration Options:

    • type: Use PrinterTypes.STAR or PrinterTypes.EPSON.
    • width: Number of characters in one line.
    • interface: The connection method (e.g., tcp://xxx.xxx.xxx.xxx).
    • characterSet: Use CharacterSet constants (e.g., CharacterSet.PC852_LATIN2).
    • removeSpecialCharacters: Boolean (default: false).
    • lineCharacter: Character used for lines (default: -).
    • breakLine: Use BreakLine constants (WORD, CHARACTERS, or NONE).
    • options: An object containing timeout (ms) for network connections (default: 3000).
    const { ThermalPrinter, PrinterTypes, CharacterSet, BreakLine } = require('node-thermal-printer');
    
    const printer = new ThermalPrinter({
      type: PrinterTypes.STAR,
      width: 48,
      interface: 'tcp://xxx.xxx.xxx.xxx',
      characterSet: CharacterSet.PC852_LATIN2,
      removeSpecialCharacters: false,
      lineCharacter: '=',
      breakLine: BreakLine.WORD,
      options: {
        timeout: 5000
      }
    });
  7. Print 1D Barcodes (STAR)

    master

    Use the printBarcode method for STAR-compatible printers. You must provide the barcode data, the STAR barcode type ID, and an optional settings object.

    Settings Options:

    • characters: Controls added under-bar characters (See Reference).
    • mode: Controls the barcode mode/dot density (See Reference).
    • height: Barcode height (0 ≤ height ≤ 255).
    var data = "TEST"        // Barcode data (string or buffer)
    var type = 7             // Barcode type (See Reference)
    var settings = {
      characters: 1,
      mode: 3,
      height: 150,
    }
    
    printer.printBarcode(data, type, settings);
  8. Print Barcodes and QR Codes

    master

    The library supports various 1D and 2D barcode types with customizable settings.

    Code128 Barcode:

    • width: "SMALL", "MEDIUM", or "LARGE".
    • height: 50 to 80.
    • text: 1 (No text), 2 (Bottom), 3 (No text inline), 4 (Bottom inline).

    QR Code:

    • cellSize: 1 to 8.
    • correction: "L", "M", "Q", or "H".
    • model: 1, 2 (standard), or 3 (Micro QR).

    PDF417:

    • rowHeight: 2 to 8.
    • width: 2 to 8.
    • correction: 1 to 40.
    • truncated: boolean.
    • columns: 1 to 30, or 0 for auto.

    MaxiCode:

    • mode: 2 (US), 3 (International), 4 (Unformatted Standard Error), 5 (Unformatted Enhanced Error), 6 (Hardware devices).
    printer.code128("Code128", {
        width: "LARGE",
        height: 80,
        text: 2
    });
    
    printer.printQR("QR Code", {
        cellSize: 3,
        correction: 'M',
        model: 2
    });
    
    printer.pdf417("PDF417", {
        rowHeight: 3,
        width: 3,
        correction: 1,
        truncated: false,
        columns: 0
    });
    
    printer.maxiCode("MaxiCode", {
        mode: 4
    });
  9. Epson 1D Barcode Type Reference

    master

    When using printBarcode with Epson printers, use the following numeric IDs for the type parameter:

    #TypePossible CharactersLength of Data
    65UPC-A0 - 911, 12
    66UPC-E0 - 96 – 8, 11, 12
    67JAN130 - 912, 13
    68JAN80 - 97, 8
    69Code390 – 9, A – Z, SP, $, %, *, +, -, ., /1 – 255
    70ITF (Interleaved 2 of 5)0 – 92 – 254 (even number)
    71CODABAR (NW-7)0 – 9, A – D, a – d, $, +, −, ., /, :2 – 255
    72CODE9300h – 7Fh1 – 255
    73CODE12800h – 7Fh2 - 255
    74GS1-128NUL – SP(7Fh)2 – 255
    75GS1 DataBar Omnidirectional0 – 913
    76GS1 DataBar Truncated0 – 913
    77GS1 DataBar Limited0 – 913
    78GS1 DataBar Expanded0 – 9, A – D, a – d, SP, !, ", %, $, ', (, ), *, +, ,, -, ., /, :, ;, <, =, >, ?, _, {2 - 255
  10. Configure Printer Interfaces

    master

    The interface option determines how the module communicates with the hardware. Supported formats include:

    ValueDescription
    tcp://192.168.0.99:9100Network printer with specific IP and port
    printer:autoAuto-selects raw system printer via printer or electron-printer modules
    printer:My Printer NameSelects a specific system printer by name
    \\.\\COM1Prints via a local serial port or file
    // Example: Network interface
    const printer = new ThermalPrinter({
      type: PrinterTypes.EPSON,
      interface: 'tcp://192.168.0.99:9100'
    });
  11. STAR Barcode Settings Reference

    master

    For STAR printers, the settings object can include characters and mode keys.

    Settings: characters

    #Description
    1No added under-bar characters. Executes line feed after printing a bar code
    2Adds under-bar characters. Executes line feed after printing a bar code
    3No added under-bar characters. Does not execute line feed after printing a bar code
    4Adds under-bar characters. Does not execute line feed after printing a bar code

    Settings: mode

    #UPC-E, UPC-A, JAN/EAN8, JAN/EAN13, Code128, Code93Code39, NW-7ITF
    1Minimum module 2 dotsNarrow: Wide = 2:6 dotsNarrow: Wide = 2:5 dots
    2Minimum module 3 dotsNarrow: Wide = 3:9 dotsNarrow: Wide = 4:10 dots
    3Minimum module 4 dotsNarrow: Wide = 4:12 dotsNarrow: Wide = 6:15 dots
    4Narrow: Wide = 2:5 dotsNarrow: Wide = 2:4 dots
    5Narrow: Wide = 3:8 dotsNarrow: Wide = 4:8 dots
    6Narrow: Wide = 4:10 dotsNarrow: Wide = 6:12 dots
    7Narrow: Wide = 2:4 dotsNarrow: Wide = 2:6 dots
    8Narrow: Wide = 3:6 dotsNarrow: Wide = 3:9 dots
    9Narrow: Wide = 4:8 dotsNarrow: Wide = 4:12 dots