diff --git a/src/client.ts b/src/client.ts old mode 100644 new mode 100755 index b13c03a..a3bf213 --- a/src/client.ts +++ b/src/client.ts @@ -6,6 +6,7 @@ import Printer from './device/printer'; import ConsolePrinterDriver from './printer-driver/console'; import FilesystemPrinterDriver from './printer-driver/filesystem-printer'; // import EscposPrinter from './printer-driver/escpos'; +import CommandPrinterDriver from './printer-driver/command-printer'; // import StarPrinterDriver from './printer-driver/star'; const readFile = promisify(fs.readFile); @@ -101,6 +102,10 @@ export default async ( // case 'escpos': // printerDriver = new EscposPrinter(); // break; + case 'command': + // Example configuration for Command printer which uses a python script + printerDriver = new CommandPrinterDriver("python3", ['../python_print/main.py']); + break; // case 'star': // printerDriver = new StarPrinterDriver(); // break; diff --git a/src/printer-driver/command-printer.ts b/src/printer-driver/command-printer.ts new file mode 100755 index 0000000..219dc37 --- /dev/null +++ b/src/printer-driver/command-printer.ts @@ -0,0 +1,71 @@ +import { PrinterDriverInterface, PrintingResult } from '.'; +import fs from 'fs'; +import os from 'os'; +import path from 'path'; +import bitmapify from '../decoder/parser/bitmapify'; +import { spawn } from 'child_process'; + +/** + * This class implements a spawning mechanism to print via any command / executable + * by providing the path to the created bitmap file. This can be used for example + * to print via a python script. + */ +export default class CommandPrinterDriver implements PrinterDriverInterface { + + /** + * The Parameters that will be passed on to the command + */ + parameters: [string]; + + /** + * The command to be executed + */ + command: string; + + constructor(command:string, parameters:[string] ){ + this.parameters = parameters; + this.command = command; + } + + /** + * @param buffer A Buffer object containing raw data encapsultated + * @returns The path to the created bitmap file. + */ + createImageFileFromBuffer(buffer: Buffer){ + const tempDir = path.join(os.tmpdir(), 'sirius-client'); + fs.mkdirSync(tempDir, { recursive: true }); + + const randomName = Math.random().toString(36); + const tempFile = path.join(tempDir, randomName + '.bmp'); + + const bitmap = bitmapify(buffer); + fs.writeFileSync(tempFile, bitmap); + console.log(`Written: ${tempFile}`); + return tempFile; + } + + async print(buffer: Buffer): Promise { + return new Promise(resolve => { + const imageFile = this.createImageFileFromBuffer(buffer); + const commandParams = [...this.parameters]; + commandParams.push(imageFile); + const process = spawn(this.command, commandParams); + + process.stdout.on('data', function (data) { + console.log('Pipe data from command ...'); + console.log(data.toString()); + }); + process.on('error', function (...args) { + console.log('Command error', args); + }); + process.on('exit', function (code, signal) { + console.log('Command exit', code, signal); + }); + process.on('close', function (code, signal) { + console.log('Command close', code, signal); + resolve(); + }); + + }); + } +}