Skip to main content

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();
  • 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