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 🙏

© 2024 – Pkg Stats / Ryan Hefner

esc-pos-printer

v1.0.11

Published

#### IMPORTANT

Downloads

268

Readme

ESC-POS-PRINTER for Javascript / Typescript

IMPORTANT

To use this library, the use of the ESC-POS Printer manager is required.

Installation

npm npm npm

npm install esc-pos-printer
# or
yarn add esc-pos-printer

Example

IMPORTANT

If the printer is not configured, the esc-pos printer manager will not be able to detect it

Please see: HOW TO SET UP PRINTER

See it live escpos-printer-example.netlify.app

EXAMPLE SOURCE CODE

import Printer from "esc-pos-printer";

const handlePrint = async () => {
  try {
    /* Create new printer  */
    const printer = new Printer();

    /* for japanese and chinese text
       const printer = new Printer(sharedPrinterName, { textAsian: true });
       or
       printer.setPrinterTextAsian(true);

       printer.text("こんにちは、これは機能します \n");
       printer.text("你好,这有效 \n");
    */

    /* for use unifont chars
       const printer = new Printer(sharedPrinterName, { textSpecial: true });
       or
       printer.setPrinterTextSpecial(true);
       
       printer.text("IT WORKS!!! :D\n");
       printer.text("Hola esto funciona\n");
       printer.text("Ողջույն, սա աշխատում է: \n");
       printer.text("こんにちは、これは機能します \n");
       printer.text("안녕하세요 이게 작동해요 \n");
       printer.text("你好,这有效 \n");
    */

    /* get Printers list  */
    const printerList = await printer.getPrinters();

    /* you can set the list in a state and use a select */
    /* printerList[0] for this example */
    printer.setPrinterName(printerList[0]);

    printer.text("IT WORKS!!! :D\n");
    printer.feed(2);
    printer.cut();
    printer.close();
    await printer.print();
  } catch (error) {
    /*  Handle error  */
  }
};

Printer Class Documentation

The Printer class in TypeScript provides a structured interface for interacting with printers

Class Overview

The Printer class encapsulates functionality to control and manage printing actions on a connected printer. It abstracts various printing operations into methods that generate a sequence of commands (results) to be executed by the printer.

Constructor

constructor(printerName?: string)

  • Description: Initializes a new instance of the Printer class.
  • Parameters:
    • printerName (optional): A string representing the name or identifier of the printer.
  • Usage:
    const printer = new Printer("MyPrinter");

Public methods

getPrinters(): Promise<string[] | []>

  • Description: Retrieves a list of available printers.
  • Returns: A promise resolving to an array of printer names.
  • Usage:
    const printerList = await printer.getPrinters();

setPrinterName(printerName: string): void

  • Description: Sets the name of the printer.
  • Parameters:
    • printerName : A string representing the name or identifier of the printer.
  • Usage:
    printer.setPrinterName("NewPrinterName");

setPrinterTextAsian(value:boolean): void

  • Description:for more compatibility with asian characters compatible with some printers
  • Parameters:
    • value : a boolean to enable or disable TextAsian
  • Usage:
    printer.setPrinterTextAsian(true);

setPrinterTextSpecial(value:boolean): void EXPERIMENTAL

  • Description: The text rendered of this print instance is going to be printed as unifont
  • Parameters:
    • value : a boolean to enable or disable TextSpecial
  • Usage:
    printer.setPrinterTextSpecial(true);

selectPrintMode(mode?: PrinterModes): void

  • Description: Sets the print mode of the printer (e.g., font size, emphasis).
  • Parameters:
    • mode (optional): A value from PrinterModes enum representing the desired print mode.
  • Usage:
    printer.selectPrintMode(PrinterModes.MODE_DOUBLE_WIDTH);

justify(mode: JustifyModes): void

  • Description: Sets the text justification for printing.
  • Parameters:
    • mode:A value from JustifyModes enum representing the desired text justification.
  • Usage:
    printer.justify(JustifyModes.justifyCenter);

printBase64Image(imageBase64: string,imageMode?:PrinterImagesModes ): void

  • Description: Prints a Base64-encoded image.
  • Parameters:
    • imageBase64:A string containing the Base64-encoded image data.
    • imageMode (optional): the size of render image example: PrinterImagesModes.IMG_DOUBLE_WIDTH
  • Usage:
    printer.printBase64Image("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA..."); // Example Base64 image

text(text: string): void

  • Description: Adds text to the print queue.
  • Parameters:
    • text:A string representing the text to print.
  • Usage:
    printer.text("Hello, world!");

barcode(value: string,mode?: BarcodeModes): void

  • Description: Adds barcode to the print queue.
  • Parameters:
    • value:A string representing the text for the barcode.
    • mode?:The barcode mode.
  • Usage:
    printer.barcode("ABC",BarcodeModes.BARCODE_CODE39);

qrCode(value: string,size?: number, model?: QrModes): void

  • Description: Adds barcode to the print queue.
  • Parameters:
    • value:A string representing the text for the qrCode.
    • size:Pixel size to use. Must be 1-16 (default 3)
    • model?:qr model "QR_MODEL_2" DEFAULT
  • Usage:
    printer.barcode("ABC",QrModes.QR_MODEL_2);

feed(value?: number): void

  • Description: Feeds the paper in the printer.
  • Parameters:
    • value(optional): The number of units to feed (default is 1 unit).
  • Usage:
    printer.feed(); // Feed one unit
    printer.feed(2); // Feed two units

setEmphasis(value: boolean): void

  • Description: Sets emphasis (bold) mode for text.
  • Parameters:
    • value: A boolean indicating whether to enable (true) or disable (false) emphasis.
  • Usage:
    printer.setEmphasis(true); // Enable emphasis

cut(): void

  • Description: Initiates a paper cut action.
  • Usage:
    printer.cut();

close(): void

  • Description: Closes the printer connection or finalizes printing.
  • Usage:
    printer.close();

print(): Promise<{ success: boolean }>

  • Description: Sends the accumulated print commands to the printer.
  • Returns: A promise resolving to an object { success: boolean } upon successful printing.
  • Usage:
    await printer.print();

Enums

PrinterModes

  • Description: Enumerates various print modes (e.g., font settings, underline).
  • Values:
    • MODE_DOUBLE_WIDTH
    • MODE_DOUBLE_HEIGHT
    • MODE_EMPHASIZED
    • MODE_FONT_A
    • MODE_FONT_B
    • MODE_UNDERLINE

JustifyModes

  • Description: Enumerates text justification modes.
  • Values:
    • justifyCenter
    • justifyLeft
    • justifyRight

MIT license

Copyright (c) 2024 yayidg22

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.