printhub
v1.0.6
Published
A javascript plugin for Bluetooth & USB printers.
Downloads
420
Maintainers
Readme
PrintHub
PrintHub is a JavaScript plugin for printing text using a Bluetooth or USB thermal printer. Demo: PrintHub Demo
Features
- Print text with various options like bold, underline, alignment, and text size.
- Print text in two columns.
- Print dashed lines.
- Print line breaks.
- Supports two paper sizes: "58mm" and "80mm".
- Supports connecting to Bluetooth thermal printers.
- Compatible with modern browsers such as Chrome, Firefox, and Edge.
- Node.js compatible.
- Supports usage via CDN.
- Supports usage via NPM.
- ES6 compatible.
Installation
Using NPM
npm install printhub
Import or require PrintHub into your project.
import PrintHub from "printhub";
or
const PrintHub = require("printhub");
Using CDN
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/index.min.js"></script>
Usage
Creating a PrintHub Instance
You can create an instance of PrintHub with or without specifying the desired paper size. Supported paper sizes are "58" and "80". If the paper size is not specified, the default is "58".
Creating a PrintHub instance with "80" paper size
let printer = new PrintHub({ paperSize: "80", });
Creating a PrintHub instance with "58" paper size
let printer = new PrintHub();
Selecting Printer Type
You can select the type of printer to use. Supported types are "bluetooth" and "usb". If the printer type is not specified, the default is "bluetooth".
Selecting "bluetooth" printer type
let printer = new PrintHub({ printerType: "bluetooth", });
Selecting "usb" printer type
let printer = new PrintHub({ printerType: "usb", });
Connecting to Printer and Printing Text
Use the connectToPrint
method to connect to a Bluetooth printer and print text. You need to provide two callback functions: onReady
and onFailed
.
| Callback | Description |
| ---------- | --------------------------------------------------------------------------------------------------------------------- |
| onReady
| Called when the printer connection is successful. You can use the print object passed to this callback to print text. |
| onFailed
| Called when the printer connection fails. You can use the message parameter to get the error message. |
How to Use PrintHub
Connect to the printer and print text.
printer.connectToPrint({ onReady: async (print) => { await print.writeText("Hello, World!"); }, onFailed: (message) => { console.log(message); }, });
Print bold text.
printer.connectToPrint({ onReady: async (print) => { await print.writeText("Hello, World!", { bold: true }); }, onFailed: (message) => { console.log(message); }, });
Print underlined text.
printer.connectToPrint({ onReady: async (print) => { await print.writeText("Hello, World!", { underline: true }); }, onFailed: (message) => { console.log(message); }, });
Print text with alignment.
printer.connectToPrint({ onReady: async (print) => { await print.writeText("Hello, World!", { align: "center" }); }, onFailed: (message) => { console.log(message); }, });
Print text with a specific size.
printer.connectToPrint({ onReady: async (print) => { await print.writeText("Hello, World!", { size: "double" }); }, onFailed: (message) => { console.log(message); }, });
Print text in two columns.
printer.connectToPrint({ onReady: async (print) => { await print.writeTextWith2Column("Name", "John Doe"); }, onFailed: (message) => { console.log(message); }, });
Print dashed lines.
printer.connectToPrint({ onReady: async (print) => { await print.writeDashLine(); }, onFailed: (message) => { console.log(message); }, });
Print line breaks.
printer.connectToPrint({ onReady: async (print) => { await print.writeLineBreak(); }, onFailed: (message) => { console.log(message); }, });
Print text with multiple options.
printer.connectToPrint({ onReady: async (print) => { await print.writeText("Hello, World!", { bold: true, underline: true, align: "center", size: "double", }); }, onFailed: (message) => { console.log(message); }, });
Print text with multiple options in two columns.
printer.connectToPrint({ onReady: async (print) => { await print.writeTextWith2Column("Name", "John Doe", { bold: true, underline: true, align: "center", size: "double", }); }, onFailed: (message) => { console.log(message); }, });
API
| Method | Description |
| --------------------------------------------- | ----------------------------------------------------------------------- |
| writeLineBreak({ count = 1 })
| Writes a line break. |
| writeDashLine()
| Writes a dashed line. |
| writeTextWith2Column(text1, text2, options)
| Writes text in two columns. |
| writeText(text, options)
| Writes text. |
| connectToPrint({ onReady, onFailed })
| Connects to the printer and calls the onReady
or onFailed
callback. |
Options for writeText
and writeTextWith2Column
Methods
| Option | Description | Default |
| ----------- | ---------------------------------------------------------------------- | -------- |
| bold
| Specifies whether the text is printed in bold. | false
|
| underline
| Specifies whether the text is printed with an underline. | false
|
| align
| Specifies text alignment. Supported values: "left", "center", "right". | left
|
| size
| Specifies the text size. Supported values: "normal", "double". | normal
|
Requirements for USB Printer
Windows
- Install Zadig.
- Connect the USB Printer to your computer.
- Open Zadig and select your USB Printer.
- Install the WinUSB driver for your USB Printer.
- Done.
Linux
- Connect the USB Printer to your computer.
- Open the terminal and run
lsusb
. - Find your USB Printer and note the vendor id and product id.
- Run
sudo modprobe usblp
. - Run
sudo echo "ATTRS{idVendor}=="YOUR_VENDOR_ID", ATTRS{idProduct}=="YOUR_PRODUCT_ID", MODE="0666", GROUP="plugdev" > /etc/udev/rules.d/99-usb-printer.rules
. - Run
sudo udevadm control --reload-rules
. - Run
sudo udevadm trigger
. - Done.
macOS
- Connect the USB Printer to your computer.
- Open the terminal and run
ls /dev/cu.*
. - Find your USB Printer and note the device name.
- Run `sudo chmod
- Run `sudo chown
- Done.
Browser Support
Desktop
| Browser | Version | Status | | ------- | ------- | ------ | | Chrome | 61 | ✔️ | | Firefox | No | ❌ | | Edge | 79 | ✔️ | | Safari | No | ❌ | | Opera | 48 | ✔️ |
Mobile
| Browser | Version | Status | | ------- | ------- | ------ | | Chrome | 61 | ✔️ | | Firefox | No | ❌ | | Safari | No | ❌ | | Opera | 45 | ✔️ | | Samsung | 8.0 | ✔️ | | WebView | No | ❌ |
Change Log
v1.0.6
- Remove unused code
v1.0.5
- Update paper size from "58mm" and "80mm" to "58" and "80"
v1.0.4
- Added instructions for USB Printer on Windows, Linux, and macOS
v1.0.3
- Fixed USB Printer not working