1.0.2 • Published 1 year ago
xml-to-escpos v1.0.2
XML to ESC/POS
Translation/Tradução
Cross platform JavaScript library that implements the thermal printer ESC / POS protocol and provides an XML interface for preparing templates for printing.
Features
- Text
- Text line
- Feed line
- Bold text
- Underline text
- Font size
- Small mode
- White mode
- Align
- QRcode
- Paper cut node
- Image (base64) (png only)
- XML with mustache
Tested manually on following environments or platforms
- React Native (Android)
- React Native (iOS)
- React Native Web
- Server side (NodeJs)
- Desktop applications (nwjs & electron)
- Other node environment (terminal)
Installation
yarn add xml-to-escpos
Or
npm install xml-to-escpos --save
Examples
From plain XML
import { EscPos } from 'escpos-xml';
const xml = `
<?xml version="1.0" encoding="UTF-8"?>
<document>
<text-line>hello world</text-line>
</document>
`;
const buffer = EscPos.getBufferFromXML(xml);
// send this buffer to a stream (eg.: bluetooth, wifi, usb, etc.)
From XML + Handlebars
import { EscPos } from 'xml-to-escpos';
const xml = `
<?xml version="1.0" encoding="UTF-8"?>
<document>
<text-line>{{foo}}</text-line>
</document>
`;
const input = {
foo: 'hello world',
};
const buffer = EscPos.getBufferFromTemplate(template, input);
// send this buffer to a stream (eg.: bluetooth, wifi, usb, etc.)
With an XML template + png image (base64)
const template = `<?xml version="1.0" encoding="UTF-8"?>
<document>
<align mode="center">
<bold>
<text-line size="1:0">{{title}}</text-line>
</bold>
<image density="d24">
{{base64PngImage}}
</image>
</align>
</document>`;
const input = {
title: 'PNG - base64',
base64PngImage: `data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAIAAACRXR/mAAAAdklEQVR4nOzQMQ2AUBAEUQInhOCDDq8EAVigQgUaEPA1TLLFFTMCNi9by35Moa5vTU3NqaFsskiySLJIskiySLJIskiySLJIskiySE1Zdf5baut579RU07dkkWSRZJFkkWSRZJFkkWSRZJFkkWSRmrJGAAAA///HQgaco1VmUwAAAABJRU5ErkJggg==`,
};
const buffer = EscPos.getBufferFromTemplate(template, input);
// send this buffer to a stream (eg.: bluetooth, wifi, usb, etc.)
TODO
- Font styles (font family)
- Barcode
- Image bitmap conversion improvements
- jpeg support
- Add example apps to repo
- Removed uglify for some reason, need to bring it back
- Improve image rendering
Common issues
If there is any delay you observe while printing with this library it is mostly due to image manipulations (try without image 😬 )
Useful links / resources
- ESC / POS Commands manual
- A blog post explaiing about printing images with ESCPOS
Similar library for serverside - node-escpos.
Limitations on the react-native framework
- FileReader.readAsArrayBuffer was not implemented.
- Most of popular image manupulation libraries does not have support for react-native. eg : jimp, Most of popular image manupulation libraries does not have support for react-native. eg : jimp, jpeg-js and sharp. We can use these libraries with some native node lib implemented in react native (some sort of polyfill).
- Use this node-libs-react-native if we need to use this library in react native (adds some mock or js implementation for fs, stream etc)
Contributions of any kind welcome! :heart: