React library for managing Epson printers with ePOS SDK support.
npm install react-epson-epos-sdkaddXmlChunk method in the Printer class. Contributions and feature requests are welcome!
PrinterProvider.
print method is asynchronous and returns a result (SUCCESS or ERROR). This provides better handling and a clearer overview of your print jobs, making it easier to manage printing operations.
bash
npm install react-epson-epos-sdk
`
or
`bash
yarn add react-epson-epos-sdk
`
---
Peer Dependencies
Ensure you have the following peer dependencies installed in your project:
- react (v17, v18, or v19)
- react-dom (v17, v18, or v19)
---
Usage
$3
Wrap your application with the PrinterProvider to manage printer connections and state:
`tsx
import { PrinterProvider } from "react-epson-epos-sdk";
import { PaperSize } from "react-epson-epos-sdk";
const App = () => {
return (
{/ Your application components /}
);
};
export default App;
`
$3
The usePrinter hook provides access to the printer context, allowing you to interact with the printer directly:
`tsx
import { usePrinter, PrintSymbolType, PrinterCutType, PrintSymbolLevel } from "react-epson-epos-sdk";
const PrintButton = () => {
const { printer, status, print } = usePrinter();
const handlePrint = async () => {
if (!printer) {
throw new Error("Printer not found!");
}
// Add text to the print job
printer.addText("Hello, Epson!", { addNewLine: true, capitalize: true });
// Add a 2D symbol (e.g., QR Code)
printer.addSymbol("https://example.com", {
type: PrintSymbolType.QRCODE_MODEL_2,
level: PrintSymbolLevel.LEVEL_M,
width: 4,
});
// Add a cut command
printer.addCut(PrinterCutType.CUT_FEED);
// Send the print job
try {
await print();
console.log("Print job sent successfully!");
} catch (error) {
console.error("Failed to send print job:", error);
}
};
return (
);
};
export default PrintButton;
`
---
API Reference
$3
| Prop | Type | Description |
| ------------- | ----------- | ----------------------------------------------------------------------- |
| printerIp | string | The IP address of the printer. |
| paperSize | PaperSize | The paper size, using the PaperSize enum. |
| isDebugMode | boolean | (Optional) Enables debug logging for printer status and unprinted data. |
$3
The usePrinter hook provides access to the following:
- printer: An instance of the Printer class.
- connection: The current connection status (CONNECTED, DISCONNECTED, etc.).
- print(): A method to send the current print job to the printer.
---
Printer Functions
The Printer class provides the following methods for building print jobs:
- addText(text: string, options?: AddTextOptions)
- Adds text to the print job with optional formatting.
- addSymbol(data: string, options: AddSymbolOptions)
- Adds a 2D symbol (e.g., QR Code, PDF417, DataMatrix) to the print job with customizable options.
- addCut(type?: PrinterCutType)
- Adds a cut command to the print job.
- addFeedLine(line: number)
- Adds a feed line to the print job.
- setTextFont(font: PrinterTextFont)
- Sets the font for the text.
- setTextStyle(style: Partial
- Sets the style for the text (e.g., bold, underline).
- setTextSize(size: TextSize)
- Sets the size of the text.
- setTextAlign(align: PrinterAlign)
- Sets the alignment of the text.
- addHorizontalLine()
- Adds a horizontal line to the print job.
- addXmlChunk(xmlChunk: string)
- Adds a raw XML chunk to the print job.
---
Enums
$3
- FONT_A
- FONT_B
- FONT_C
- FONT_D
- FONT_E
$3
- CUT_NO_FEED
- CUT_FEED
- CUT_RESERVE
- FULL_CUT_NO_FEED
- FULL_CUT_FEED
- FULL_CUT_RESERVE
$3
- COLOR_NONE
- COLOR_1
- COLOR_2
- COLOR_3
- COLOR_4
$3
- ALIGN_LEFT
- ALIGN_CENTER
- ALIGN_RIGHT
$3
- SIZE_80MM
- SIZE_58MM
$3
- PDF417_standard
- PDF417_truncated
- QRCODE_MODEL_1
- QRCODE_MODEL_2
- MAXICODE_MODE_2
- MAXICODE_MODE_3
- MAXICODE_MODE_4
- MAXICODE_MODE_5
- MAXICODE_MODE_6
- GS1_DATABAR_STACKED
- GS1_DATABAR_STACKED_OMNIDIRECTIONAL
- GS1_DATABAR_EXPANDED_STACKED
- AZTECCODE_FULLRANGE
- AZTECCODE_COMPACT
- DATAMATRIX_SQUARE
- DATAMATRIX_RECTANGLE_8
- DATAMATRIX_RECTANGLE_12
- DATAMATRIX_RECTANGLE_16
$3
- LEVEL_0 to LEVEL_8 (PDF417 error correction levels)
- LEVEL_L, LEVEL_M, LEVEL_Q, LEVEL_H (QR Code error correction levels)
- DEFAULT`