npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

webbluetooth-receipt-printer

v1.1.1

Published

This is an library that allows you to print to a Bluetooth connected receipt printer using WebBluetooth.

Downloads

39

Readme

WebBluetoothReceiptPrinter

This is an library that allows you to print to a Bluetooth connected receipt printer using WebBluetooth.

What does this library do?

In order to print a receipt on a receipt printer you need to build the receipt and encode it as in the ESC/POS or StarPRNT language. You can use the ThermalPrinterEncoder library for this. You end up with an array of raw bytes that needs to be send to the printer. One way to do that is using this library and WebBluetooth.

How to use it?

Load the webbluetooth-receipt-printer.umd.js file from the dist directory in the browser and instantiate a WebBluetoothReceiptPrinter object.

<script src='webbluetooth-receipt-printer.umd.js'></script>

<script>

    const receiptPrinter = new WebBluetoothReceiptPrinter();

</script>

Or import the webbluetooth-receipt-printer.esm.js module:

import WebHIDBarcodeScanner from 'webbluetooth-receipt-printer.esm.js';

const receiptPrinter = new WebBluetoothReceiptPrinter();

Connect to a receipt printer

The first time you have to manually connect to the receipt printer by calling the connect() function. This function must be called as the result of an user action, for example clicking a button. You cannot call this function on page load.

function handleConnectButtonClick() {
    receiptPrinter.connect();
}

In the future, there is a way to reconnect to already connected printers. This functionality is still behind a flag in currently shipping browsers. To do it you can simply call the reconnect() function. You have to provide an object with the id of the previously connected receipt printer in order to find the correct printer and connect to it again. You can get this data by listening to the connected event and store it for later use. It is recommended to call this button on page load to prevent having to manually connect to a previously connected device.

receiptPrinter.reconnect(lastUsedDevice);

If there are no receipt printers connected that have been previously connected, or the id does not match up, this function will do nothing.

To find out when a receipt printer is connected you can listen for the connected event using the addEventListener() function.

receiptPrinter.addEventListener('connected', device => {
    console.log(`Connected to ${device.name} (#${device.id})`);

    printerLanguage = device.language;
    printerCodepageMapping = device.codepageMapping;

    /* Store device for reconnecting */
    lastUsedDevice = device;
});

The callback of the connected event is passed an object with the following properties:

  • type Type of the connection that is used, in this case it is always bluetooth.
  • name The name of the receipt printer.
  • id A unique id for the receipt printer. To be used to reconnect to the printer at a later time.
  • language Language of the printer, which can be either esc-pos or star-prnt. This can be used as an option for ThermalPrinterEncoder to encode in the correct language for the printer.
  • codepageMapping Code page mapping of the printer, which can be used as an option for ThermalPrinterEncoder to map non-ascii characters to the correct codepage supported by the printer.

Commands

Once connected you can use the following command to print receipts.

Printing receipts

When you want to print a receipt, you can call the print() function with an array, or a typed array with bytes. The data must be properly encoded for the printer.

For example:

/* Encode the receipt */

let encoder = new ThermalPrinterEncoder({
    language:  printerLanguage,
    codepageMapping: printerCodepageMapping
});

let data = encoder
    .initialize()
    .text('The quick brown fox jumps over the lazy dog')
    .newline()
    .qrcode('https://nielsleenheer.com')
    .encode();

/* Print the receipt */

receiptPrinter.print(data);

License

MIT