# xml-to-escpos

> ESC/POS with XML interface

Latest version **1.0.2** (published 2022-11-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install xml-to-escpos
pnpm add xml-to-escpos
yarn add xml-to-escpos
bun add xml-to-escpos
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2022-11-27 |
| First published | 2022-11-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=4.0.0 |
| Dependencies | 6 |
| Unpacked size | 63.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Maintainers | alex.madeira |
| Keywords | escpos, esc, pos, xml, printer |

## Links

- npm: https://www.npmjs.com/package/xml-to-escpos
- Repository: https://github.com/alexmadeira/xml-to-escpos
- Homepage: https://ajar-krill-39c.notion.site/Xml-to-Esc-Pos-482565e31bbd440997319622857675bb
- Issues: https://github.com/alexmadeira/xml-to-escpos/issues
- npm.io page: https://npm.io/package/xml-to-escpos

## Dependencies (6)

- [pngjs](https://npm.io/package/pngjs.md) ^6.0.0
- [buffer](https://npm.io/package/buffer.md) ^6.0.3
- [ndarray](https://npm.io/package/ndarray.md) ^1.0.19
- [mustache](https://npm.io/package/mustache.md) ^4.1.0
- [xml-parser](https://npm.io/package/xml-parser.md) ^1.2.1
- [mutable-buffer](https://npm.io/package/mutable-buffer.md) ^4.0.2

## Alternatives

- [babylon](https://npm.io/package/babylon.md) — 5.1M weekly downloads
- [csscolorparser](https://npm.io/package/csscolorparser.md) — 3.7M weekly downloads
- [expr-eval-fork](https://npm.io/package/expr-eval-fork.md) — 1.5M weekly downloads
- [@leeoniya/ufuzzy](https://npm.io/package/@leeoniya/ufuzzy.md) — 247.7K weekly downloads
- [xml-parser](https://npm.io/package/xml-parser.md) — 78.4K weekly downloads

## Recent versions

- 1.0.2 (latest) — 2022-11-27
- 1.0.1 — 2022-11-27
- 1.0.0 — 2022-11-27

## README

# **XML to ESC/POS**

### Translation/Tradução

[🇧🇷](README-pt_br.md)
[🇺🇸](README.md)

---

Cross platform JavaScript library that implements the thermal printer ESC / POS protocol and provides an XML interface for preparing templates for printing.

## **Features**

- [x] Text
- [x] Text line
- [x] Feed line
- [x] Bold text
- [x] Underline text
- [x] Font size
- [x] Small mode
- [x] White mode
- [x] Align
- [x] QRcode
- [x] Paper cut node
- [x] Image (base64) (png only)
- [x] XML with mustache

## **Tested manually on following environments or platforms**

- [x] React Native (Android)
- [x] React Native (iOS)
- [x] React Native Web
- [x] Server side (NodeJs)
- [x] Desktop applications (nwjs & electron)
- [x] Other node environment (terminal)

# **Installation**

```bash
yarn add xml-to-escpos
```

Or

```bash
npm install xml-to-escpos --save
```

## **Examples**

### **From plain XML**

```tsx
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**

```tsx
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)**

```tsx
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](notion://www.notion.so/resources/ESCPOS_Command_Manual.pdf)
- A [blog post](https://www.visuality.pl/posts/thermal-printer-protocols-for-image-and-text#:~:text=How%20can%20we%20print%20an,command%20language%20of%20thermal%20printers) explaiing about printing images with ESCPOS
- Similar library for serverside - [node-escpos](https://github.com/song940/node-escpos).

- _Limitations on the react-native framework_
  - [FileReader.readAsArrayBuffer](https://github.com/facebook/react-native/issues/21209) was not implemented.
  - Most of popular image manupulation libraries does not have support for react-native. eg : [jimp](https://www.npmjs.com/package/jimp), Most of popular image manupulation libraries does not have support for react-native. eg : [jimp](https://www.npmjs.com/package/jimp), [jpeg-js](https://www.npmjs.com/package/jpeg-js) and [sharp](https://www.npmjs.com/package/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](https://www.npmjs.com/package/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:

---
_Source: https://npm.io/package/xml-to-escpos · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
