- Notifications
You must be signed in to change notification settings - Fork9
Print to a Bluetooth connected receipt printer using WebBluetooth
License
NielsLeenheer/WebBluetoothReceiptPrinter
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
This is an library that allows you to print to a Bluetooth connected receipt printer using WebBluetooth.
This library is part of@point-of-sale, a collection of libraries for interfacing browsers and Node with Point of Sale devices such as receipt printers, barcode scanners and customer facing displays.
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 theReceiptPrinterEncoder
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.
Load thewebbluetooth-receipt-printer.umd.js
file from thedist
directory in the browser and instantiate aWebBluetoothReceiptPrinter
object.
<scriptsrc='webbluetooth-receipt-printer.umd.js'></script><script>constreceiptPrinter=newWebBluetoothReceiptPrinter();</script>
Or import thewebbluetooth-receipt-printer.esm.js
module:
importWebHIDBarcodeScannerfrom'webbluetooth-receipt-printer.esm.js';constreceiptPrinter=newWebBluetoothReceiptPrinter();
The first time you have to manually connect to the receipt printer by calling theconnect()
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.
functionhandleConnectButtonClick(){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 thereconnect()
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 theconnected
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 theconnected
event using theaddEventListener()
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 theconnected
event is passed an object with the following properties:
type
Type of the connection that is used, in this case it is alwaysbluetooth
.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 eitheresc-pos
orstar-prnt
. This can be used as an option forReceiptPrinterEncoder
to encode in the correct language for the printer.codepageMapping
Code page mapping of the printer, which can be used as an option forReceiptPrinterEncoder
to map non-ascii characters to the correct codepage supported by the printer.
Once connected you can use the following command to print receipts.
When you want to print a receipt, you can call theprint()
function with an array, or a typed array with bytes. The data must be properly encoded for the printer.
For example:
/* Encode the receipt */letencoder=newReceiptPrinterEncoder({language:printerLanguage,codepageMapping:printerCodepageMapping});letdata=encoder.initialize().text('The quick brown fox jumps over the lazy dog').newline().qrcode('https://nielsleenheer.com').encode();/* Print the receipt */receiptPrinter.print(data);
This library has been created by Niels Leenheer under theMIT license. Feel free to use it in your products. The development of this library is sponsored by Salonhub.
About
Print to a Bluetooth connected receipt printer using WebBluetooth
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Uh oh!
There was an error while loading.Please reload this page.