sdk-printer
v0.0.7
Published
printer for kiosk
Readme
sdk-printer
printer for kiosk
Install
npm install sdk-printer
npx cap syncAPI
connect()close()resetFont()getStatus()outputString(...)outputStringLn(...)printBarcode(...)printQRcode(...)cutPaper()printFormattedText(...)printText(...)doFunction(...)
connect()
connect() => Promise<{ success: boolean; message: string; }>Returns: Promise<{ success: boolean; message: string; }>
close()
close() => Promise<void>resetFont()
resetFont() => Promise<void>getStatus()
getStatus() => Promise<{ status1: number; status2: number; }>Returns: Promise<{ status1: number; status2: number; }>
outputString(...)
outputString(options: { str: string; }) => Promise<void>| Param | Type |
| ------------- | ----------------------------- |
| options | { str: string; } |
outputStringLn(...)
outputStringLn(options: { str: string; }) => Promise<void>| Param | Type |
| ------------- | ----------------------------- |
| options | { str: string; } |
printBarcode(...)
printBarcode(options: { type: number; str: string; }) => Promise<void>| Param | Type |
| ------------- | ------------------------------------------- |
| options | { type: number; str: string; } |
printQRcode(...)
printQRcode(options: { data: string; }) => Promise<void>| Param | Type |
| ------------- | ------------------------------ |
| options | { data: string; } |
cutPaper()
cutPaper() => Promise<void>printFormattedText(...)
printFormattedText(options: { text: string; }) => Promise<void>| Param | Type |
| ------------- | ------------------------------ |
| options | { text: string; } |
printText(...)
printText(options: { text: string; logoBase64?: string; }) => Promise<void>| Param | Type |
| ------------- | --------------------------------------------------- |
| options | { text: string; logoBase64?: string; } |
doFunction(...)
doFunction(options: { func: number; param1: number; param2: number; }) => Promise<void>| Param | Type |
| ------------- | -------------------------------------------------------------- |
| options | { func: number; param1: number; param2: number; } |
Constants for doFunction
export const TX_CONSTANTS = {
// --- TRẠNG THÁI ON/OFF ---
TX_ON: 1,
TX_OFF: 0,
// --- 1. CỠ CHỮ (TX_FONT_SIZE) ---
// param1: Chiều rộng (0-7), param2: Chiều cao (0-7)
// 0: Bình thường (1x), 1: 2x, 2: 3x ... 7: 8x
TX_FONT_SIZE: 1,
TX_SIZE_1X: 0,
TX_SIZE_2X: 1,
TX_SIZE_3X: 2,
TX_SIZE_4X: 3,
TX_SIZE_8X: 7,
// --- 2. GẠCH CHÂN (TX_FONT_ULINE) ---
TX_FONT_ULINE: 2,
// --- 3. IN ĐẬM (TX_FONT_BOLD) ---
TX_FONT_BOLD: 3,
// --- 4. CHỌN FONT (TX_SEL_FONT) ---
TX_SEL_FONT: 4,
TX_FONT_A: 0, // 12x24 (Font chuẩn)
TX_FONT_B: 1, // 9x17 (Font nhỏ)
// --- 5. XOAY 90 ĐỘ (TX_FONT_ROTATE) ---
TX_FONT_ROTATE: 5,
// --- 6. CĂN LỀ (TX_ALIGN) ---
TX_ALIGN: 6,
TX_ALIGN_LEFT: 0,
TX_ALIGN_CENTER: 1,
TX_ALIGN_RIGHT: 2,
// --- 7. CHẾ ĐỘ TIẾNG VIỆT/TRUNG (TX_CHINESE_MODE) ---
TX_CHINESE_MODE: 7,
// --- 10. ĐẨY GIẤY (TX_FEED) ---
// Đơn vị: 0.125mm (Ví dụ feed 10mm thì param1 = 80)
TX_FEED: 10,
// --- 12. CẮT GIẤY (TX_CUT) ---
TX_CUT: 12,
TX_CUT_FULL: 0, // Cắt đứt hoàn toàn (Có check black mark)
TX_CUT_PARTIAL: 1, // Cắt 1 phần (Có check black mark)
TX_PURECUT_FULL: 2, // Cắt đứt hoàn toàn (Không check black mark - Thường dùng cái này)
TX_PURECUT_PARTIAL: 3, // Cắt 1 phần (Không check black mark)
// --- 14. KHOẢNG CÁCH DÒNG (TX_LINE_SP) ---
TX_LINE_SP: 14,
// --- 15. ĐẢO MÀU ĐEN TRẮNG (TX_BW_REVERSE) ---
TX_BW_REVERSE: 15,
// --- 16. LỘN NGƯỢC ĐẦU (TX_UPSIDE_DOWN) ---
TX_UPSIDE_DOWN: 16,
// --- 22. IN LOGO (TX_PRINT_LOGO) ---
TX_PRINT_LOGO: 22,
TX_LOGO_1X1: 0, // Kích thước thật
// --- 23. MÃ VẠCH (BARCODE) ---
TX_BARCODE_HEIGHT: 23, // Chiều cao
TX_BARCODE_WIDTH: 24, // Độ rộng nét (>=2)
TX_BARCODE_FONT: 25, // Vị trí chữ số HRI
TX_BAR_FONT_NONE: 0,
TX_BAR_FONT_UP: 1,
TX_BAR_FONT_DOWN: 2, // Chữ nằm dưới mã vạch
TX_BAR_FONT_BOTH: 3,
// --- 26. ĐẨY GIẤY NGƯỢC (TX_FEED_REV) ---
TX_FEED_REV: 26,
// --- 27. QR CODE SIZE (TX_QR_DOTSIZE) ---
TX_QR_DOTSIZE: 27, // 2 < param1 < 10 (Thường dùng 6)
// --- 28. QR ERROR LEVEL (TX_QR_ERRLEVEL) ---
TX_QR_ERRLEVEL: 28,
TX_QR_ERRLEVEL_L: 0x31,
TX_QR_ERRLEVEL_M: 0x32, // Trung bình (Thường dùng)
TX_QR_ERRLEVEL_Q: 0x33,
TX_QR_ERRLEVEL_H: 0x34, // Cao nhất
};Examples
Kết nối máy in
const connectPrinter = async () => {
try {
const res = await PrinterPlugin.connect();
if (res.success) {
console.log('✅ Kết nối thành công:', res.message);
}
} catch (err: any) {
console.error('❌ Kết nối thất bại:', err.message);
}
};In hóa đơn
// Hàm helper in 2 cột căn đều
const printTwoCol = async (left: string, right: string) => {
const TOTAL_WIDTH = 32;
const spaceLen = Math.max(0, TOTAL_WIDTH - left.length - right.length);
const line = left + ' '.repeat(spaceLen) + right;
await PrinterPlugin.outputStringLn({ str: line });
};
// In hóa đơn mẫu
const printReceipt = async () => {
try {
await PrinterPlugin.resetFont();
// Header - Center, Bold, Large
await PrinterPlugin.doFunction({
func: TX_CONSTANTS.TX_ALIGN,
param1: TX_CONSTANTS.TX_ALIGN_CENTER,
param2: 0,
});
await PrinterPlugin.doFunction({
func: TX_CONSTANTS.TX_FONT_BOLD,
param1: TX_CONSTANTS.TX_ON,
param2: 0,
});
await PrinterPlugin.doFunction({
func: TX_CONSTANTS.TX_FONT_SIZE,
param1: TX_CONSTANTS.TX_SIZE_2X,
param2: TX_CONSTANTS.TX_SIZE_2X,
});
await PrinterPlugin.outputStringLn({ str: 'RECEIPT' });
// Reset size & bold
await PrinterPlugin.doFunction({
func: TX_CONSTANTS.TX_FONT_SIZE,
param1: TX_CONSTANTS.TX_SIZE_1X,
param2: TX_CONSTANTS.TX_SIZE_1X,
});
await PrinterPlugin.doFunction({
func: TX_CONSTANTS.TX_FONT_BOLD,
param1: TX_CONSTANTS.TX_OFF,
param2: 0,
});
await PrinterPlugin.outputStringLn({ str: 'Date: 20/12/2025' });
// Items
await PrinterPlugin.doFunction({
func: TX_CONSTANTS.TX_ALIGN,
param1: TX_CONSTANTS.TX_ALIGN_LEFT,
param2: 0,
});
await PrinterPlugin.outputStringLn({ str: '--------------------------------' });
await printTwoCol('Item', 'Price');
await PrinterPlugin.outputStringLn({ str: '--------------------------------' });
const items = [
{ name: 'Coffee', price: 50000 },
{ name: 'Tea', price: 30000 },
];
for (const item of items) {
await printTwoCol(item.name, item.price.toString());
}
// Total
await PrinterPlugin.outputStringLn({ str: '--------------------------------' });
await PrinterPlugin.doFunction({
func: TX_CONSTANTS.TX_FONT_BOLD,
param1: TX_CONSTANTS.TX_ON,
param2: 0,
});
await printTwoCol('Total', '80000');
await PrinterPlugin.doFunction({
func: TX_CONSTANTS.TX_FONT_BOLD,
param1: TX_CONSTANTS.TX_OFF,
param2: 0,
});
// Footer
await PrinterPlugin.doFunction({
func: TX_CONSTANTS.TX_ALIGN,
param1: TX_CONSTANTS.TX_ALIGN_CENTER,
param2: 0,
});
await PrinterPlugin.outputStringLn({ str: 'Thank you!' });
// Cut paper
await PrinterPlugin.doFunction({
func: TX_CONSTANTS.TX_FEED,
param1: 100,
param2: 0,
});
await PrinterPlugin.cutPaper();
console.log('✅ In thành công');
} catch (err: any) {
console.error('❌ Lỗi in:', err.message);
}
};