Werkt
This commit is contained in:
92
print.ts
92
print.ts
@@ -1,5 +1,7 @@
|
||||
import Printer from "@node-escpos/core";
|
||||
import Network from "@node-escpos/network-adapter";
|
||||
import { Buffer } from "buffer";
|
||||
import _, {floor} from "lodash";
|
||||
import _ from "lodash";
|
||||
|
||||
const statics: any = {
|
||||
'ESC': '1B',
|
||||
@@ -10,33 +12,38 @@ const mmToInch = 0.0393701;
|
||||
// 26x51 label, or 1" x 2"
|
||||
|
||||
const pageWidth = getPageDimensionParameters(2);
|
||||
const pageHeight = getPageDimensionParameters(1);
|
||||
const pageHeight = getPageDimensionParameters(1 / 2);
|
||||
const command = `
|
||||
ESC i a 00h
|
||||
ESC @
|
||||
ESC i L 01h
|
||||
ESC i L 00h
|
||||
ESC ( C 02h 00h ${pageWidth.mL} ${pageWidth.mH}
|
||||
ESC $ CBh 00h
|
||||
ESC ( V 02h 00h ${pageHeight.mL} ${pageHeight.mH}
|
||||
ESC k 0Bh
|
||||
ESC X 00h 40h 00h
|
||||
ESC i t0 r0 h E0h 00h w1 e0 z0 f1 B 123456789 \\
|
||||
ESC i t0 r0 e0 z0 f1 B 123456789 \\
|
||||
FF
|
||||
`;
|
||||
|
||||
const grocy_template_print = `
|
||||
^II
|
||||
^TS001
|
||||
grcy:p:13:60bf8b5244b04
|
||||
09h
|
||||
Test Product
|
||||
09h
|
||||
01-01-2026
|
||||
^FF
|
||||
const barcodeHeight = getPageDimensionParameters(1 / 2);
|
||||
|
||||
const grocycodeTest = `
|
||||
ESC @
|
||||
ESC i a 00h
|
||||
ESC i D 6h 00h 28h 28h 00h 00h 00h 00h 00h grcy:p:13:60bf8b5244b04 \\\\\\
|
||||
FF
|
||||
`
|
||||
|
||||
|
||||
console.warn('output: ', parseEscToHex(command))
|
||||
const grocy_template_hex = parseEscToHex(`
|
||||
^II
|
||||
^TS001 `) + parseTextToHex(`THT 02-01-2026`) +
|
||||
parseEscToHex(`09h`) + parseTextToHex(`Test Product met lange naam`) +
|
||||
parseEscToHex(`
|
||||
09h
|
||||
grcy:p:13:60bf8b5244b04
|
||||
^FF
|
||||
`);
|
||||
|
||||
function inchToDots(value: number, dpi = 203) {
|
||||
const dots = value * dpi;
|
||||
@@ -63,20 +70,45 @@ function getPageDimensionParameters(inInches: number) {
|
||||
function parseEscToHex(text: string): string {
|
||||
|
||||
return text.trim().split(/\s+/)
|
||||
.map(chunk => {
|
||||
if (_.has(statics, chunk)) {
|
||||
// Found static mapping
|
||||
return statics[chunk]
|
||||
}
|
||||
|
||||
if(chunk.length == 3 && chunk.endsWith('h')) {
|
||||
// parse as hex
|
||||
return chunk.slice(0, -1)
|
||||
}
|
||||
|
||||
return Buffer.from(chunk).toString('hex')
|
||||
|
||||
})
|
||||
.map(chunk => parseTextToHex(chunk))
|
||||
.join(' ')
|
||||
}
|
||||
|
||||
}
|
||||
function parseTextToHex(text: string): string {
|
||||
if (_.has(statics, text)) {
|
||||
// Found static mapping
|
||||
return statics[text]
|
||||
}
|
||||
|
||||
if(text.length == 3 && text.endsWith('h')) {
|
||||
// parse as hex
|
||||
return text.slice(0, -1)
|
||||
}
|
||||
|
||||
return Buffer.from(text).toString('hex')
|
||||
}
|
||||
|
||||
|
||||
///
|
||||
|
||||
const device = new Network('172.20.0.112');
|
||||
|
||||
device.open(async function (err) {
|
||||
if (err) {
|
||||
// handle error
|
||||
return
|
||||
}
|
||||
|
||||
// encoding is optional
|
||||
const options = {encoding: "GB18030" /* default */}
|
||||
let printer = new Printer(device, options);
|
||||
|
||||
// Path to png image
|
||||
|
||||
printer
|
||||
// .raw(parseEscToHex(grocy_template_print))
|
||||
.raw(grocy_template_hex)
|
||||
// .size(1, 1)
|
||||
// .text('EAN13 barcode example')
|
||||
.close()
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user