Files
brother-printer-webhook/print.ts
Marc Fokkert db886eb8c4 Werkt
2025-01-26 15:05:45 +01:00

115 lines
2.4 KiB
TypeScript

import Printer from "@node-escpos/core";
import Network from "@node-escpos/network-adapter";
import { Buffer } from "buffer";
import _ from "lodash";
const statics: any = {
'ESC': '1B',
'FF': '0C'
}
const mmToInch = 0.0393701;
// 26x51 label, or 1" x 2"
const pageWidth = getPageDimensionParameters(2);
const pageHeight = getPageDimensionParameters(1 / 2);
const command = `
ESC i a 00h
ESC @
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 e0 z0 f1 B 123456789 \\
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
`
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;
const margin = 6 * mmToInch * dpi;
return Math.round(dots - margin);
}
function toHex(value: number) {
return value.toString(16).padStart(2, '0') + 'h';
}
function getPageDimensionParameters(inInches: number) {
const dots = inchToDots(inInches);
const mH = Math.floor(dots / 265);
const mL = dots - mH * 265;
return {
mH: toHex(mH), mL: toHex(mL)
}
}
function parseEscToHex(text: string): string {
return text.trim().split(/\s+/)
.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()
});