UNPKG

epos-print-xml

Version:

A package to produce the xml needed for Epson Server Direct Print

70 lines (69 loc) 2.99 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.eposPrintRequest = exports.eposPrintJob = exports.qrCode = exports.image = exports.cut = exports.feed = exports.text = exports.PrintDataNode = exports.sanitise = void 0; const sanitise = (xmlString) => xmlString.replace(/[<>&'"]/g, (unsafeString) => { switch (unsafeString) { case '<': return '&lt;'; case '>': return '&gt;'; case '&': return '&amp;'; case "'": return '&apos;'; case '"': return '&quot;'; default: return unsafeString; } }); exports.sanitise = sanitise; class PrintDataNode { constructor(tagName, att = {}, children = [], text = '') { this.text = ''; this.tagName = tagName; this.att = att; this.text = text; this.children = children; } xml(rootNode = false) { const nodeXml = `<${this.tagName}` + (Object.keys(this.att).length > 0 ? ` ${Object.entries(this.att) .map((att) => { const [attName, value] = att; return `${attName}="${value.toString()}"`; }) .join(' ')}` : '') + (this.children.length !== 0 || this.text ? '>' + (0, exports.sanitise)(this.text) + this.children.map((child) => child.xml()).join('') + `</${this.tagName}>` : '/>'); return rootNode ? `<?xml version="1.0" encoding="utf-8"?> ${nodeXml}` : nodeXml; } } exports.PrintDataNode = PrintDataNode; const text = (text = '', att = {}) => new PrintDataNode('text', att, [], text); exports.text = text; const feed = (lines = 1) => new PrintDataNode('feed', { line: lines }); exports.feed = feed; const cut = () => new PrintDataNode('cut', { type: 'feed' }); exports.cut = cut; const image = (width, height, base64Image, align = 'center') => new PrintDataNode('image', { width, height, align }, undefined, base64Image); exports.image = image; const qrCode = (text, width = '10') => new PrintDataNode('symbol', { type: 'qrcode_model_1', width, align: 'center' }, undefined, text); exports.qrCode = qrCode; const eposPrintJob = (printData, devId = 'local_printer', timeout = 10000) => { return new PrintDataNode('ePOSPrint', undefined, [ new PrintDataNode('Parameter', undefined, [ new PrintDataNode('devid', undefined, undefined, devId), new PrintDataNode('timeout', undefined, undefined, timeout.toString()), ]), new PrintDataNode('PrintData', undefined, [ new PrintDataNode('epos-print', { xmlns: 'http://www.epson-pos.com/schemas/2011/03/epos-print' }, printData), ]), ]); }; exports.eposPrintJob = eposPrintJob; const eposPrintRequest = (printJobs) => new PrintDataNode('PrintRequestInfo', undefined, printJobs); exports.eposPrintRequest = eposPrintRequest;