To use this library, the use of the ESC-POS Printer manager is required.
npm install esc-pos-printer
# or
yarn add esc-pos-printer
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
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 */
}
};
The Printer
class in TypeScript provides a structured interface for interacting with printers
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.
- 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");
- Description: Retrieves a list of available printers.
- Returns: A promise resolving to an array of printer names.
- Usage:
const printerList = await printer.getPrinters();
- Description: Sets the name of the printer.
- Parameters:
printerName
: A string representing the name or identifier of the printer.
- Usage:
printer.setPrinterName("NewPrinterName");
- Description:for more compatibility with asian characters compatible with some printers
- Parameters:
value
: a boolean to enable or disable TextAsian
- Usage:
printer.setPrinterTextAsian(true);
- 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);
- Description: Sets the print mode of the printer (e.g., font size, emphasis).
- Parameters:
mode
(optional): A value fromPrinterModes
enum representing the desired print mode.
- Usage:
printer.selectPrintMode(PrinterModes.MODE_DOUBLE_WIDTH);
- Description: Sets the text justification for printing.
- Parameters:
mode
:A value fromJustifyModes
enum representing the desired text justification.
- Usage:
printer.justify(JustifyModes.justifyCenter);
- 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
- Description: Adds text to the print queue.
- Parameters:
text
:A string representing the text to print.
- Usage:
printer.text("Hello, world!");
- 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);
- 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);
- 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
- 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
- Description: Initiates a paper cut action.
- Usage:
printer.cut();
- 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();
- 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
- Description: Enumerates text justification modes.
- Values:
justifyCenter
justifyLeft
justifyRight
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.