-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8e388fe
commit 823805c
Showing
12 changed files
with
502 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
lib/* | ||
node_modules/* | ||
coverage/* | ||
**/lib | ||
**/node_modules | ||
coverage/* | ||
**/dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"name": "examples", | ||
"version": "1.0.0", | ||
"description": "Using in Nodejs env", | ||
"main": "index.js", | ||
"repository": "http://github.com/hitz-group/xml-escpos-helper.git", | ||
"license": "MIT", | ||
"private": false, | ||
"scripts": { | ||
"clean": "rm -rf ./dist && rm -rf ./tsconfig.build.tsbuildinfo", | ||
"compile": "yarn clean && tsc -p ./tsconfig.json", | ||
"print:basic": "yarn compile && yarn node ./dist/basic.js", | ||
"print:image": "yarn compile && yarn node ./dist/printImage.js", | ||
"print:table": "yarn compile && yarn node ./dist/printTable.js", | ||
"print:barcode": "yarn compile && yarn node ./dist/printBarcode.js" | ||
}, | ||
"dependencies": { | ||
"@tillpos/xml-escpos-helper": "^0.1.8", | ||
"table": "^6.6.0" | ||
}, | ||
"devDependencies": { | ||
"typescript": "^4.2.4" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { sendDataToPrinter } from './network'; | ||
import { TEMPLATES } from './templates'; | ||
|
||
const printBasic = async () => { | ||
const input = { | ||
title: 'Sample', | ||
thankyouNote: 'Welcome...!' | ||
}; | ||
|
||
await sendDataToPrinter(input, TEMPLATES.BASIC); | ||
}; | ||
|
||
printBasic(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import net from 'net'; | ||
import { EscPos } from '@tillpos/xml-escpos-helper'; | ||
const PRINTERS = [{ device_name: 'Epson', host: '192.168.0.8', port: 9100 }]; | ||
|
||
const connectToPrinter = ( | ||
host: string, | ||
port: number, | ||
buffer: Buffer, | ||
): Promise<unknown> => { | ||
return new Promise((res: (value: unknown) => void, rej) => { | ||
let device = new net.Socket(); | ||
|
||
device.on('close', () => { | ||
if (device) { | ||
device.destroy(); | ||
device = null; | ||
} | ||
res(true); | ||
return; | ||
}); | ||
|
||
device.on('error', rej); | ||
|
||
device.connect(port, host, () => { | ||
device.write(buffer); | ||
device.emit('close'); | ||
}); | ||
}); | ||
}; | ||
|
||
export const sendDataToPrinter = async (input: any, template: string) => { | ||
const { host, port } = PRINTERS[0]; | ||
const buffer = EscPos.getBufferFromTemplate(template, input); | ||
try { | ||
await connectToPrinter(host, port, (buffer as unknown) as Buffer); | ||
} catch (err) { | ||
console.log('some error', err); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { sendDataToPrinter } from './network'; | ||
import { TEMPLATES } from './templates'; | ||
|
||
const printBarCode = async () => { | ||
const input = { | ||
barcode: '12345678', | ||
}; | ||
|
||
await sendDataToPrinter(input, TEMPLATES.BAR_CODE); | ||
}; | ||
|
||
printBarCode(); |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { sendDataToPrinter } from './network'; | ||
import { TEMPLATES } from './templates'; | ||
import { getBorderCharacters, table } from 'table'; | ||
|
||
const printTable = async () => { | ||
const versions = [ | ||
['React', '16.8'], | ||
['Angular', '9'], | ||
['Ember', '3.16'], | ||
]; | ||
|
||
const tableData = table(versions, { | ||
border: getBorderCharacters(`void`), | ||
drawHorizontalLine: () => { | ||
return false; | ||
}, | ||
}); | ||
|
||
await sendDataToPrinter({ tableData }, TEMPLATES.TABLE); | ||
}; | ||
|
||
printTable(); |
Oops, something went wrong.